4

I have a query that looks like this:

cls.objects.filter(name__in=lookup_values).values():

The problem is that sometimes I want to filter by name but other times I want to filter by, say, officer_number or customer_number. Is there a way for me to dynamically set the whatever__in part of the query so it's not hard-coded?

Jason Swett
  • 43,526
  • 67
  • 220
  • 351
  • see - http://stackoverflow.com/questions/310732/in-django-how-does-one-filter-a-queryset-with-dynamic-field-lookups - e.g. kwargs = {'%s__%s' % ('name', 'in'): 'A'} ? – JamesO Mar 15 '11 at 15:57

1 Answers1

10

Do you mean something like:

args = {'id__in':[1]}

qs = Something.objects.filter(**args)
sra
  • 23,820
  • 7
  • 55
  • 89
Davo
  • 221
  • 1
  • 4