13

i have a django app that retrieve all subjects from a single table of users. i've also implemented an input search form, this is the query performed:

all_soggs =     Entity.objects.filter(lastname__istartswith=request.GET['query_term']).order_by('lastname')
if(all_soggs.count()==0):
    all_soggs = Entity.objects.filter(firstname__istartswith=request.GET['query_term']).order_by('firstname')

as you can see the query first search for matching items by lastname, and then by firstname. this works until i insert the complete name 'firstaname lastname' or 'lastname firstname', in this case there's no results. how can i modify the query to make a better search?

thanks - luke

4 Answers 4

17

Copy/paste from: https://stackoverflow.com/a/17361729/1297812

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
0
7

You need Q objects and you also need to split your query into separate terms (since no first name will match the full string "Firstname Lastname").

Here's an idea to match any first or last name starting with either "Firstname" or "Lastname" in the search "Firstname Lastname".

This is a generic search - adjust the query to suit your specific needs!

Edit: oops, I really don't like using reduce since it looks confusing, but these need to be ORed together and we can't do a more verbose version because the number of terms is unknown.

import operator
from django.db.models import Q

search_args = []
for term in request.GET['query_term'].split():
    for query in ('first_name__istartswith', 'last_name__istartswith'):
        search_args.append(Q(**{query: term}))

all_soggs = Entity.objects.filter(reduce(operator.or_, search_args))

To clarify how to use Q objects, given the search "Firstname Lastname" the previous query is equal to:

Entity.objects.filter(
    Q(first_name__istartswith="Firstname") | Q(last_name__istartswith="Firstname") |
    Q(first_name__istartswith="Lastname") | Q(last_name__istartswith="Lastname")
    )
3

This is a fairly old question but I just ran into the same problem and I thought I would share a more elegant solution.

from django.db.models import Value as V
from django.db.models.functions import Concat

from ..models import User

def find_user_by_name(search_str) -> QuerySey[User]:
    q = User.objects.annotate(full_name=Concat('first_name', V(' '), 'last_name'))
    q = q.filter(full_name__icontains=search_str)
    return q

This is the only solution that I have found which lets gives the behaviour I wanted, IE seaching with a full name string with a space ("John Doe") and with a partial string ("John Do").

1
  • Thanks for your excellent answer. May i know if this solution comes with any significant performance cost. i.e. is it efficient to use in a large scale application or will it will be better to just add a new field to the database and save the concatenated strings from first_name and last_name as a separate field. Commented Jan 28 at 7:06
0

Similar question:Querying full name in Django

query = request.GET.get('query')
entities = []

try:
    firstname = query.split(' ')[0]
    lastname  = query.split(' ')[1]
    entities += Entity.objects.filter(firstname__icontains=firstname,lastname__icontains=lastname)
    entities += Entity.objects.filter(firstname__icontains=lastname,lastname__icontains=firstname)

entities = set(entities)
2
  • What's the point of answering the question twice and referencing your own answer?
    – Omri Luzon
    Commented May 26, 2017 at 19:08
  • 1
    According to the rules of stack overflow you have to give complete answer even if you are referencing a link, because links can go broke
    – atx
    Commented May 26, 2017 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.