3

I would like to pass one or many GET variables to filter a queryset. I have tried the following code to create a dictionary of the variables and apply the filter, but testing with two variables it appears to only be filtering on the final dictionary variables.

for k,v in mydict.items():
    qs = mymodel.objects.filter(**{"%s__contains" % k: v})

Can anyone point me in the right direction as to where I am going wrong?

1 Answer 1

2

You are creating a new queryset in each iteration from scratch instead of chaining them. Try changing your code to:

qs = mymodel.objects.all()
for k, v in mydict.items():
    qs = qs.filter(**{"%s__contains" % k: v})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.