Complete the code to import Q from Django's models.
from django.db.models import [1]
The Q object is imported from django.db.models to build complex queries.
Complete the code to filter books with title 'Django' or author 'Alice'.
from django.db.models import Q books = Book.objects.filter(Q(title='Django') [1] Q(author='Alice'))
Use the | operator to combine Q objects with OR logic.
Fix the error in combining Q objects to exclude books published before 2020.
from django.db.models import Q books = Book.objects.filter(~Q(published_year [1] 2020))
The ~ operator negates the Q object. To exclude books published before 2020, use < inside Q.
Fill both blanks to filter users with first name 'John' and last name not 'Doe'.
from django.db.models import Q users = User.objects.filter(Q(first_name=[1]) [2] ~Q(last_name='Doe'))
Use 'John' as the first name and combine Q objects with & for AND logic.
Fill all three blanks to filter products with price greater than 100, category 'Books', and name containing 'Python'.
from django.db.models import Q products = Product.objects.filter(Q(price__[1]=100) [2] Q(category=[3]) & Q(name__icontains='Python'))
Use gt for greater than, combine with & for AND, and use 'Books' as category string.