Dear Christian,

Thanks a lot, this is exactly what I needed and it works like a charm!
For the moment, I am using a list, as you suggested, since I am not
using the keys of the dictionary.  And I had to make a slight change,
since what I needed was an AND operator, not or, so I ended up with:
    results = Person.objects.filter(reduce(operator.and_, qs))
which is blazingly fast and does exactly what I need.  Great!

All the best,

Chris

On Oct 18, 7:46 pm, Christian Joergensen <[EMAIL PROTECTED]> wrote:
> chris wrote:
> > Dear django users,
>
> > Being rather new to django, I can't wrap my head around the
> > following:
> > I am trying to execute a query from a search form.  The search form
> > has three fields, Names, Dates and Places.  I want to AND the queries,
> > so that if Names and Places are filled in, I want to return only
> > records that match both.
>
> >  My idea was to build up the condition in a dictionary while looking
> > through the fields returned from the form like this:
>
> >     if places:
> >         Q['places'] = Q(persplace__icontains=places)
>
> >     if names:
> >         Q['names'] =  Q(persname__icontains=names)
>
> > etc and then concatenate and execute the whole at the end:
>
> >     results = Person.objects.filter("|".join(Q.values())
>
> You are almost right. Istead of "|".join() you need to use something
> like reduce and operator.or_:
>
> import operator
> results = Person.objects.filter(reduce(operator.or_, Q.values()))
>
> The builtin function `reduce` will apply the first argument to every
> element in the sequence given as the second argument and reduce it to a
> single value, just as if you wrote: var1 | var2 | var3
>
> Another thing is - unless you use the key of the dictionary, a list
> might be more appropriate for this ;)
>
> Regards,
>
> --
> Christian Joergensenhttp://www.technobabble.dk
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to