Django uses Object Relational Mapper for the querying database. It’s good for most cases, but sometimes, you need to run more complex queries. For that, the Django ORM ship with
Q objects and it can construct more complex database queries.
At the very beginning, when I study the Django material, I find it very hard to follow the docs. Already ORM is very complicated to understand, but the Q documentation makes it much harder to grasp. I know what it is now, and I’m here to tell you how easy it is to use it. Without further due, let’s walk what Q objects are and how to use it.
For the regular queries, we use filter() function. For example, to query all the article title that starts with “How to” we need to write this in filter function.
Now we can use a Q object between these standard queries like this.
from django.db.models import Q Article.objects.filter(Q(title__startswith="How to"))
You have to think of Q objects like the normal function, and then the rest of the implementation is straightforward.
Now you know what Q objects are and how to use it in filter function. This time for you to understand the features of the Q objects.
When you write the filter function by a comma-separated, they considered as the AND methods. Now with the help of Q objects, you can write OR methods too. For example, I’m writing the simple filter function that returns article title starts with How or What.
Article.objects.filter( Q(title__startswith="How") | Q(title__startswith="What") )
You can also use Q objects to get, exclude. And combine these, you can make complex queries.