> My model has firstname and lastname as separate fields. In a search
> box if a person types in one name say "Bunny" then based on the
> following query they will find Bunny:
>
> qset = (
> Q(firstname__icontains=query) |
> Q(lastname__icontains=query) |
> )
>
> If a person types in "Bunny Foo" to find first and last name they
> won't find anything using the above query. How can I concatenate
> firstname || lastname to make contactname and use it in the qset?
The typical way to do this is to split the search-string into
words on which you want to search. You also have to decide
whether you want to require that both terms match (AND) or a
match can be found either way (OR):
search = "Bunny Foo"
terms = search.split()
# terms = re.findall(r'\w+', search)
qset = Q()
for term in terms:
qset_inner = Q()
for (field_to_search, how) in (
('firstname', 'icontains'),
('lastname', 'icontains'),
):
name = "%s__%s" % (field_to_search, how)
qset_inner |= Q(**{name: term})
qset &= qset_inner # AND them together
#qset |= qset_inner # OR them together
-tim
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---