0
0
Djangoframework~10 mins

Q objects for complex queries in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import Q from Django's models.

Django
from django.db.models import [1]
Drag options to blanks, or click blank then click option'
AF
BCount
CValue
DQ
Attempts:
3 left
💡 Hint
Common Mistakes
Importing F instead of Q
Forgetting to import Q
Importing from the wrong module
2fill in blank
medium

Complete the code to filter books with title 'Django' or author 'Alice'.

Django
from django.db.models import Q
books = Book.objects.filter(Q(title='Django') [1] Q(author='Alice'))
Drag options to blanks, or click blank then click option'
Aand
B&
C|
Dor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'and' or 'or' keywords instead of operators
Using '&' which means AND
Using Python logical operators instead of bitwise
3fill in blank
hard

Fix the error in combining Q objects to exclude books published before 2020.

Django
from django.db.models import Q
books = Book.objects.filter(~Q(published_year [1] 2020))
Drag options to blanks, or click blank then click option'
A<
B==
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operator
Not negating the Q object properly
Using Python 'not' instead of '~'
4fill in blank
hard

Fill both blanks to filter users with first name 'John' and last name not 'Doe'.

Django
from django.db.models import Q
users = User.objects.filter(Q(first_name=[1]) [2] ~Q(last_name='Doe'))
Drag options to blanks, or click blank then click option'
A'John'
B|
C&
D'Jane'
Attempts:
3 left
💡 Hint
Common Mistakes
Using OR operator instead of AND
Using wrong string for first name
Not negating last name condition
5fill in blank
hard

Fill all three blanks to filter products with price greater than 100, category 'Books', and name containing 'Python'.

Django
from django.db.models import Q
products = Product.objects.filter(Q(price__[1]=100) [2] Q(category=[3]) & Q(name__icontains='Python'))
Drag options to blanks, or click blank then click option'
Agt
B&
C'Books'
Dlt
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lt' instead of 'gt' for price
Using OR operator instead of AND
Not quoting the category string