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':
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'.
The following code is source of Q class:
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.
We are performing the or operation using operator.or_
To use and operations simply execute:
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
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 imagesMore...