1π
β
A possible solution to your question can be worked out if your database supports full text search. I use Postgresql (see documentation) and was able to make the following query work:
print User.objects.all().extra(where =
["to_tsvector(coalesce(first_name) || coalesce(last_name))
@@ to_tsquery('BarackObama')"])
You will note that I used the full name in the query. My knowledge of text search is limited and I donβt know if there is a way to do the equivalent of __startswith
(implemented using LIKE
).
I suspect that this would be an overkill for your needs. You might be better off adding a custom field or custom method or a combination of the two to implement this.
π€Manoj Govindan
1π
With the current Django ORM, no, it doesnβt exist. You do have access to the Q object query handler.
User.objects.filter(Q(firstname__startswith=name.split(" ", 1)[0]), Q(lastname__startswith=name.split(" ", 1)[1]))
π€Andrew Sledge
- [Answered ]-How to make a string visible on a webpage using Django form fields
- [Answered ]-How to handle error in Django?
- [Answered ]-Django UnicodeDecodeError in model object
- [Answered ]-Django Site_ID's and Sitemaps
- [Answered ]-DRF update resource with serializer with source argument
Source:stackexchange.com