0
0
Djangoframework~10 mins

Why relationships model real-world data in Django - Test Your Understanding

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

Complete the code to define a Django model with a foreign key relationship.

Django
class Book(models.Model):
    author = models.ForeignKey([1], on_delete=models.CASCADE)
    title = models.CharField(max_length=100)
Drag options to blanks, or click blank then click option'
AAuthor
Bmodels.CharField
CBook
DForeignKey
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong model name in ForeignKey
Forgetting to import the related model
2fill in blank
medium

Complete the code to add a many-to-many relationship between students and courses.

Django
class Student(models.Model):
    courses = models.[1](Course)
    name = models.CharField(max_length=50)
Drag options to blanks, or click blank then click option'
AForeignKey
BManyToManyField
CCharField
DOneToOneField
Attempts:
3 left
💡 Hint
Common Mistakes
Using ForeignKey instead of ManyToManyField
Not specifying the related model
3fill in blank
hard

Fix the error in the model relationship by completing the code.

Django
class Profile(models.Model):
    user = models.OneToOneField([1], on_delete=models.CASCADE)
    bio = models.TextField()
Drag options to blanks, or click blank then click option'
AUser
BProfile
Cmodels.TextField
DForeignKey
Attempts:
3 left
💡 Hint
Common Mistakes
Linking to the wrong model
Using ForeignKey instead of OneToOneField
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each course name to the number of students enrolled, filtering courses with more than 5 students.

Django
course_counts = {course.name: [1] for course in courses if [2] > 5}
Drag options to blanks, or click blank then click option'
Acourse.students.count()
Blen(course.students)
Ccourse.students.count
Dcourse.students
Attempts:
3 left
💡 Hint
Common Mistakes
Using len() on a related manager
Using the attribute without calling count()
5fill in blank
hard

Fill all three blanks to create a queryset filtering authors who have written more than 3 books and order them by name.

Django
authors = Author.objects.annotate(book_count=[1]).filter([2]__gt=[3]).order_by('name')
Drag options to blanks, or click blank then click option'
ACount('book')
Bbook_count
C3
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong annotation syntax
Filtering on the wrong field
Not ordering correctly