[Django]-Django queryset filter question

2๐Ÿ‘

โœ…

A simple solution will be to add another field complete_name on your model. On save you will update this field by concatenate first_name and last_name fields without any space (you need to strip all spaces from the concatenation result). Then you will do your query on this field with the search_term but with spaces also stripped.

Simple example to give you the general idea:

class Person(models.Model):
    first_name = CharField(...)
    last_name = CharField(...)
    complete_name = CharField(...)

    def save(self, *args, **kwargs):
        complete_name = '%s%s' % (self.first_name, self.last_name)
        self.complete_name = complete_name.replace(' ', '')
        super(Person, self).save(*args, **kwargs)

results = Person.objects.filter(complete_name__icontains=search_term.replace(' ', ''))
๐Ÿ‘คEtienne

6๐Ÿ‘

If you want to search in first AND lastname you can simply use:

db_persons.objects.filter(first_name='Will', last_name='Smith')

Use Q objects if you want to search in first_name OR last_name:

from django.db.models.query_utils import Q
db_persons.objects.filter(Q(first_name='will') | Q(last_name='will'))

Update according comment: A simple solution could look like this:

search_string = 'will smith'
query_list = search_string.split()
db_persons.objects.filter(Q(first_name__in=query_list) | Q(last_name__in=query_list))

If you use mysql, you could use search.

๐Ÿ‘คReto Aebersold

Leave a comment