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
Written by Franco Mariluis
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#