Hello Tim,

I gave it a try, no errors, but also no results for the Contact.  I
think I have a problem in the resultsc statement.  How do I filter out
both qset3 and qset3_inner?

    query = request.GET['q']
    terms = query.split()
    # terms = re.findall(r'\w+', query)
    if query:   # will eventually add the split to this section
        qset = (
            Q(pubtitlestrip__icontains=query) |
            Q(pubauthors__icontains=query)    |
            Q(journal__journal__icontains=query)
        )
        pubresults = Publication.objects.exclude(active = 0).filter
(qset).distinct()
    qset3=Q()
    for term in terms:
        qset3_inner = Q()
        for (field_to_search, how) in (
            ('firstname', 'icontains'),
            ('lastname', 'icontains'),
            ):
            name = "%s__%s" % (field_to_search, how)
            qset3_inner |= Q(**{name: term})
            qset3 &= qset3_inner  # AND them together
        resultsc = Contact.objects.filter(qset3).distinct()

Thanks!!

On Apr 1, 1:12 pm, Tim Chase <[email protected]> wrote:
> > 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to