Issue
I have one django app, In which I search for a name from first_name and last_name. Which is working for me using Q tag.
from django.db.models import Q
def find_user_by_name(query_name):
qs = User.objects.all()
for term in query_name.split():
qs = qs.filter( Q(first_name__icontains = term) | Q(last_name__icontains = term))
return qs
But I'm facing one issue regarding ordering the data not coming as I want.
For example: search_word = 'Kia' The result I got after the search is:
{
"first_name": "Katja",
"last_name": "Tukiainen",
},
{
"first_name": "Kia",
"last_name": "Reijonen",
},
{
"first_name": "Sanna",
"last_name": "Kiander",
}
]
But I want a result in which first starts with Kia first, Result like:
{
"first_name": "Kia",
"last_name": "Reijonen",
},
{
"first_name": "Katja",
"last_name": "Tukiainen",
},
{
"first_name": "Sanna",
"last_name": "Kiander",
}
]
Please help me regarding this, I try this with startswith but not working for me. Thanks in advance.
Solution
You can work with an .union(…)
[Django-doc]:
def find_user_by_name(query_name):
qs = User.objects.all()
q1 = Q(*[
Q(('first_name__istartswith', term)) | Q(('last_name__istartswith', term))
for term in query_name.split()
])
q2 = Q(*[
Q(('first_name__icontains', term)) | Q(('last_name__icontains', term))
for term in query_name.split()
])
return qs.filter(q1).union(qs.filter(q2))
Answered By - Willem Van Onsem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.