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 Joergensen http://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 -~----------~----~----~----~------~----~------~--~---