Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django queryset filter question

I have a model I want to filter with a search term, which is usually a name. However in my database first_name and last_name are two separate fields.

e.g.

search_term = 'Will Sm'

db_persons record: first_name = 'Will', last_name = 'Smith'

the search term would be able to retrieve this record.

How can I achieve that?

db_persons.objects.filter(__?__)

UPDATE:

Looking for a way to concatenate fields and querying the concatenated result without raw sql

like image 535
zentenk Avatar asked Feb 28 '26 12:02

zentenk


2 Answers

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.

like image 85
Reto Aebersold Avatar answered Mar 03 '26 06:03

Reto Aebersold


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(' ', ''))
like image 34
Etienne Avatar answered Mar 03 '26 06:03

Etienne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!