> What is the best way to create a queryset for this without doing tons > of if statements, like: > > if request.GET['first_id'] > if request.GET['second_id'] > if request.GET['third_id'] > queryset = > Resource.objects.filter(first__id__exact=request.GET['first_id'], > second__id__exact=request.GET['second_id'], > third__id__exact=request.GET['third_id']) > else > queryset = > Resource.objects.filter(first__id__exact=request.GET['first_id'], > second__id__exact=request.GET['second_id']) > else > queryset = > Resource.objects.filter(first__id__exact=request.GET['first_id']) > elseif > #... so on
You might try this: kwargs = {} for key in ['first_id', 'second_id', 'third_id']: if request.GET[key]: kwargs[key] = request.GET[key] Resource.objects.filter(**kwargs) (i didnt test it, but thats the idea i guess ...) > That just seems like the worst way to do it. Can I pass a dictionary > to it somehow? What's the best way to do something like this? > > Hope this wasn't extremely confusing. Let me know if you need any > other information from me. > > Thanks a lot, > Stephen Mizell > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---