Q() Objects

AND, OR, NOT এই জাতীয় কমপ্লেক্স কোয়েরি করার জন্য Q() ব্যবহার করা হয়।

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=50)
    published_year = models.PositiveIntegerField()
    genre = models.CharField(max_length=50)

OR Example

from django.db.models import Q

# Retrieve books that match the criteria
books = Book.objects.filter(Q(published_year__gte=2000) | Q(genre='Science Fiction'))

And Example

# Retrieve books that match both conditions using AND
books = Book.objects.filter(Q(published_year__gte=2000) & Q(genre='Science Fiction'))

NOT Example

from django.db.models import Q

# Retrieve books that do not match the condition using NOT
books = Book.objects.filter(~Q(published_year__gte=2000))

Last updated