Scott: Very cool, didn't know that was possible. I'm sure this does not hurt
performance much compared to the other method shown above?

Needless to say it works great!

Thanks much

On Thu, Jun 12, 2008 at 5:58 AM, Scott Moonen <[EMAIL PROTECTED]> wrote:

> You can take advantage of the fact that query sets are not executed until
> they are inspected:
>
> 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
>
> On Thu, Jun 12, 2008 at 5:42 AM, caustic <[EMAIL PROTECTED]> wrote:
>
>>
>>
>>
>> On Jun 12, 10:55 am, truebosko <[EMAIL PROTECTED]> wrote:
>> > I have a card store and I would like to add 3 options a user can
>> > search by. Year, Brand, and Card Title
>> >
>> > Normally this would be easy but my dilemna here is:
>> > - All of these are optional, and if they do not search for a certain
>> > one, the query should ignore that filter entirely and display in full
>> > (So if none of the filters are active I display ALL cards)
>> >
>> > So basically what I am thinking of was:
>> >
>> > if request.POST.get('brand'):
>> > Q(brand=request.POST.get('brand')
>> >
>> > if request.POST.get('year'):
>> > Q(year=request.POST.get('year')
>> >
>> > etc.
>> >
>> > cards = Card.objects.filter(... Now how do I get those Q objects into
>> > here ??).order_by('pk')
>>
>> Try
>> cards = Card.objects.filter(Q1&Q2)
>>
>> Alternatively you can use a dictionary and ** magic:
>> filter = dict(brand=request.POST.get('brand'),
>> year=request.POST.get('year'))
>> cards = Card.objects.filter(**filter)
>>
>>
>
>
> --
> http://scott.andstuff.org/ | http://truthadorned.org/
>
> >
>

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