>> cards = Card.objects.all()
>> if request.POST.get('brand') :
>>   cards = cards.filter(brand = request.POST.get('brand'))
>> if request.POST.get('year') :
>>   cards = cards.filter(year = request.POST.get('year'))
>
> Scott: Very cool, didn't know that was possible. I'm sure this
> does not hurt

This can even be dumped in a loop, using dictionary kwargs which 
makes it more DRY:

   cards = Card.objects.all()
   for field in ('brand', 'year', 'color', 'size'):
     if field in request.POST:
       cards = cards.filter(**{
         field: request.POST.get(field)
         })

This separates out the common code, and reduces the field-names 
to just being entries in a list for which the same action is 
performed for each.

Given that they're ANDed together, you might even be able to just 
build the dictionary, and then unpack that as your filter:

   fieldnames = ('brand', 'year', 'color', 'size')
   kwargs = dict(
     (field, request.POST.get(field))
     for field in fieldnames
     if field in request.POST
     )
   cards = Card.objects.filter(**kwargs)

This *could* be reduced to a single (much more opaque) statement, 
but I usually prefer to keep a little readability compared to

   cards = Card.objects.filter(**dict(
     (field, request.POST.get(field))
     for field in ('brand', 'year', 'color', 'size')
     if field in request.POST
     ))

I haven't delved into the new QS-RF code to see whether the 
resulting queries differ vastly in meaning/efficiency, but I 
remember there being a subtle difference between chained filters 
vs. multiple filters applied at the same time (though that may 
have only applied to related models).

-tim




--~--~---------~--~----~------------~-------~--~----~
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