๐Ÿ“ Django

Q Objects in Django ORM

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
289
๐ŸŒณ
Level
Advanced

Q objects allow you to build complex query conditions using OR, AND, and NOT.

The Problem Without Q

# Regular filter โ€” AND only
Task.objects.filter(status='todo', priority=3)
# WHERE status='todo' AND priority=3
# This is not possible directly:
Task.objects.filter(status='todo' OR priority=3)  # SyntaxError!

Basic Usage

from django.db.models import Q

# OR
Task.objects.filter(Q(status='todo') | Q(status='in_progress'))

# AND (same as a regular filter)
Task.objects.filter(Q(status='todo') & Q(priority__gte=3))

# NOT
Task.objects.filter(~Q(status='done'))

Complex Combinations

# (status is todo OR in_progress) AND (priority is high OR deadline is past)
Task.objects.filter(
    (Q(status='todo') | Q(status='in_progress')) &
    (Q(priority=3) | Q(due_date__lt=today))
)

Dynamic Filter Building

def search_tasks(query=None, status=None, user=None):
    q = Q()
    if query:
        q &= Q(title__icontains=query) | Q(description__icontains=query)
    if status:
        q &= Q(status=status)
    if user:
        q &= Q(owner=user)
    return Task.objects.filter(q)

In annotate

from django.db.models import Count, Q

Project.objects.annotate(
    done_count=Count('tasks', filter=Q(tasks__status='done')),
    todo_count=Count('tasks', filter=Q(tasks__status='todo')),
)

Comparing Two Fields

# Tasks where updated_at > created_at (i.e. they have been modified)
from django.db.models import F
Task.objects.filter(updated_at__gt=F('created_at'))

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Class-Based Views in Django

CBVs use classes instead of functions. The built-in CBVs implement common CRUD patterns.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 275
๐Ÿ“

Search in Django

You can start with simple Django ORM filters and move to PostgreSQL features when you...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 277
๐Ÿ“

Swagger Documentation with drf-spectacular

drf-spectacular generates an OpenAPI 3.0 schema and Swagger UI for DRF.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 561
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Django RequestLab: from HTTP to secure production Open course curriculum