Answer the question
In order to leave comments, you need to log in
How to implement search by full name, having two separate fields in the database (first_name, last_name)?
Hello?
How to canonically release search.
There is a search query (it is not known in advance what will be entered)
There are two fields first_name,
last_name The FIRST NAME value is in firt_name, the LAST NAME value is in last_name.
Is it correct to break the search query by spaces and look for results for each word, or is there some more "direct" implementation for DJANGO?
Answer the question
In order to leave comments, you need to log in
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
I don’t know how for django, but for sql I would do the query like this (you need to break it with a space):
SELECT * FROM table
WHERE last_name = 'ФАМИЛИЯ' AND firt_name= 'ИМЯ'
SELECT * FROM table
WHERE concat(last_name, ' ', firt_name) = 'ФАМИЛИЯ ИМЯ'
SELECT * FROM table
WHERE lower(concat(last_name, ' ', firt_name)) = lower('ФАМИЛИЯ ИМЯ')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question