Operator or and and with Q

linkaiyi
Follow

using operator and_ and operation or

When connecting several Q expressions, we should use operator.or or operator.and

import operator

Objects.objects.filter(operator.or_(Q(field1=8), Q(field2__gte=19)))

otherwiese None value will cause bad results

build querystring from Dictionary

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’)

Object has 0 attachments

Was this article helpful?

This article is viewed 13 times!

Recent Viewed Articles

Related Articles

0 Comments

Leave a Comment

Support