Last Updated: February 25, 2016
·
8.476K
· fmariluis

Pass a QueryDict to a Django QuerySet

Let's suppose you have a captured QueryDict (for example, something you took from an admin url after applying a filter).

For example:
q = QueryDict('name=John&lastname=Smith')

Obviously in a real case the QueryDict would be the result of request.GET.

We can do this:
People.objects.filter(**q.dict())

Thanks to the magic of dictionary unpacking, the last line will have the same result as:
People.objects.filter(name='John', lastname='Smith')

The dict() method was added en Django 1.4.
https://docs.djangoproject.com/en/1.5/ref/request-response/#querydict-objects