'Django Query with and without comma
I have a .csv file with only one column (name) and multiple rows containing strings formatted like:
- "name, surname"
- "name surname"
I loop through this file and check if they exist in my database
the only problem is that in my database, everything is formatted like:
- "name, surname"
so the query without a comma could not be found unless I do something like this:
Model.objects.filter(name__search=row["name"])
But this takes a very long time to load..
is there any other way?
Solution 1:[1]
Have you tried with contains? See https://docs.djangoproject.com/en/4.0/ref/models/querysets/#contains-1
It may be faster if you do:
Model.objects.filter(name__contains=row["name"])
You can also try to use icontains if you want to search for case-insensitive text. See https://docs.djangoproject.com/en/4.0/ref/models/querysets/#icontains
If none of this alternatives work for you, another option would be to update the save method in your Model so you can save the data in the format you like and get consistent results there.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mtzd |
