Shawn, man thanks!  I actually had no idea about the pipe vs. ampersand
distinction.  I went ahead and changed my code to the following:

def search(request):
    query = request.GET.get('q','')
    querywords = query.split(' ')
    lname=''
    for word in querywords[1:-1]:
        lname += word+' '
    lname += querywords[-1]
    if query:
        if len(querywords)>1:
            qset = (
                Q(first_name__icontains=querywords[0]) &
                Q(last_name__icontains=lname)|
                Q(first_name__icontains=querywords[0])|
                Q(last_name__icontains=querywords[0])
            )
            results = Player.objects.filter(qset).distinct()
            results = results.order_by('last_name','first_name')
        else:
            qset = (
                Q(first_name__icontains=querywords[0])|
                Q(last_name__icontains=querywords[0])
            )
            results = Player.objects.filter(qset).distinct()
            results = results.order_by('last_name','first_name')
    else:
        results = []
    return render(request,'search.html', {
        'results': results,
        'query': query,
        'querywords':querywords
    })

This seems to work; I just need to find a way to separate exact matches
from the rest.  I'm guessing it will involve some logic to forgo the
additional queries if I can find a result for
Q(first_name__exact=querywords[0])
& Q(last_name__exact=lname), but I'm not yet sure how to structure it.

By the way, does anyone know what the default sorting order is for the
results?  You may have noticed I forced a sort order by last name and then
first name, but I find it annoying that I had to do this even though my
primary keys are in this same order.  I thought the default sort order was
by pk?

cheers,
Guillaume

On Fri, Mar 16, 2012 at 11:29 AM, Shawn Milochik <sh...@milochik.com> wrote:

> Look at how the Q objects are being used in the example and it's clear why
> that is. It's using the pipe (|) to do an "or" query.
>
> If you want to change how the search works you'll have to add more code:
>
> 1. Split the search parameters on whitespace.
> 2. Create Q objects for searching in either or both fields for the values
> and use pipe (|) and ampersand (&) to "or" or "and" them as needed.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to django-users+unsubscribe@**
> googlegroups.com <django-users%2bunsubscr...@googlegroups.com>.
> For more options, visit this group at http://groups.google.com/**
> group/django-users?hl=en<http://groups.google.com/group/django-users?hl=en>
> .
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to