Q object encapsulates a SQL expression in a Python object that can be used in database-related operations. Using Q objects we can make complex queries with less and simple code.
For example, this Q object filters whether the question starts wiht 'what':
from django.db.models import Q Q(question__startswith='What')
Q objects are helpfull for complex queries because they can be combined using logical operators and(&), or(|), negation(~)
For example, this statement returns if the question starts with 'who' or with 'what'.
Q(question__startswith='Who') | Q(question__startswith='What')
Note:>If the operator is not included then by default 'AND' operator is used
class Q(tree.Node): AND = 'AND' OR = 'OR' default = AND def __init__(self, *args, **kwargs): super(Q, self).__init__(children=list(args) + list(six.iteritems(kwargs))) def _combine(self, other, conn): if not isinstance(other, Q): raise TypeError(other) obj = type(self)() obj.connector = conn obj.add(self, conn) obj.add(other, conn) return obj def __or__(self, other): return self._combine(other, self.OR) def __and__(self, other): return self._combine(other, self.AND) def __invert__(self): obj = type(self)() obj.add(self, self.AND) obj.negate() return obj
As you can interpret from above code, we have three operators 'or', 'and' and invert(negation) and the default operator is AND.
This is an interesting feature as we can use the operator module to create dynamic queries.
import operator from django.db.models import Q from your_app.models import your_model_object q_list = [Q(question__startswith='Who'), Q(question__startswith='What')] your_model_object.objects.filter(reduce(operator.or_, q_list))
We are performing the or operation using operator.or_
your_model_object.objects.filter(reduce(operator.and_, q_list))
Q objects not only simplify complex queries, they are very handy for dynamic filtering.
To Know more about our Django CRM(Customer Relationship Management) Open Source Package. Check Code
Micropyramid is a software development and cloud consulting partner for enterprise businesses across the world. We work on python, Django, Salesforce, Angular, Reactjs, React Native, MySQL, PostgreSQL, Docker, Linux, Ansible, git, amazon web services. We are Amazon and salesforce consulting partner with 5 years of cloud architect experience. We develop e-commerce, retail, banking, machine learning, CMS, CRM web and mobile applications.
Django-CRM :Customer relationship management based on Django
Django-blog-it : django blog with complete customization and ready to use with one click installer Edit
Django-webpacker : A django compressor tool
Django-MFA : Multi Factor Authentication
Docker-box : Web Interface to manage full blown docker containers and images
More...