Bird
Raised Fist0
Djangoframework~10 mins

Model relationships preview in Django - Interactive Code Practice

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

Complete the code to define a one-to-many relationship where each Book belongs to one Author.

Django
class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.[1](Author, on_delete=models.CASCADE)
Drag options to blanks, or click blank then click option'
AForeignKey
BManyToManyField
COneToOneField
DCharField
Attempts:
3 left
💡 Hint
Common Mistakes
Using ManyToManyField instead of ForeignKey for one-to-many
Using CharField for relationships
2fill in blank
medium

Complete the code to define a many-to-many relationship between Student and Course.

Django
class Student(models.Model):
    name = models.CharField(max_length=50)
    courses = models.[1](Course)
Drag options to blanks, or click blank then click option'
AManyToManyField
BForeignKey
COneToOneField
DTextField
Attempts:
3 left
💡 Hint
Common Mistakes
Using ForeignKey which only allows one side to have many
Using OneToOneField which restricts to one-to-one
3fill in blank
hard

Fix the error in the code to correctly link Profile to User with a one-to-one relationship.

Django
class Profile(models.Model):
    user = models.[1](User, on_delete=models.CASCADE)
Drag options to blanks, or click blank then click option'
AForeignKey
BOneToOneField
CManyToManyField
DCharField
Attempts:
3 left
💡 Hint
Common Mistakes
Using ForeignKey which allows multiple Profiles per User
Using ManyToManyField which allows many Profiles per User and vice versa
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each Author's name to the count of their books.

Django
book_counts = {author.name: [1] for author in authors if [2] > 0}
Drag options to blanks, or click blank then click option'
Aauthor.book_set.count()
Bauthor.books.count()
Clen(author.book_set.all())
Dauthor.book_count
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-existent attribute like author.books
Using len() on a queryset without calling all()
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension mapping each Course's title to the number of enrolled students for courses with more than 2 students.

Django
course_students = {course.[1]: [2] for course in courses if course.students.[3] > 2}
Drag options to blanks, or click blank then click option'
Atitle
Bcourse.students.count()
Ccount()
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using course.name instead of course.title
Forgetting to call count() on the related manager

Practice

(1/5)
1. Which Django model field type represents a many-to-one relationship?
easy
A. ForeignKey
B. OneToOneField
C. ManyToManyField
D. CharField

Solution

  1. Step 1: Understand relationship types in Django models

    Django uses ForeignKey for many-to-one, OneToOneField for one-to-one, and ManyToManyField for many-to-many relationships.
  2. Step 2: Match the field to many-to-one relationship

    The ForeignKey field connects many instances of one model to a single instance of another model, representing many-to-one.
  3. Final Answer:

    ForeignKey -> Option A
  4. Quick Check:

    Many-to-one = ForeignKey [OK]
Hint: ForeignKey always means many-to-one relation [OK]
Common Mistakes:
  • Confusing OneToOneField with ForeignKey
  • Thinking ManyToManyField is many-to-one
  • Using CharField for relationships
2. Which of the following is the correct syntax to define a one-to-one relationship in a Django model?
easy
A. user = models.ForeignKey(User, on_delete=models.CASCADE)
B. email = models.CharField(max_length=100)
C. groups = models.ManyToManyField(Group)
D. profile = models.OneToOneField(User, on_delete=models.CASCADE)

Solution

  1. Step 1: Identify the field for one-to-one relationship

    The OneToOneField is used to create a one-to-one link between two models.
  2. Step 2: Check the syntax correctness

    The syntax profile = models.OneToOneField(User, on_delete=models.CASCADE) correctly defines a one-to-one relationship with proper arguments.
  3. Final Answer:

    profile = models.OneToOneField(User, on_delete=models.CASCADE) -> Option D
  4. Quick Check:

    OneToOneField syntax = profile = models.OneToOneField(User, on_delete=models.CASCADE) [OK]
Hint: OneToOneField uses on_delete argument like ForeignKey [OK]
Common Mistakes:
  • Using ForeignKey instead of OneToOneField
  • Omitting on_delete argument
  • Confusing ManyToManyField syntax
3. Given these models:
class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

What will book.author.name return if book is a Book instance?
medium
A. The name of the author related to the book
B. The title of the book
C. An error because author is a ForeignKey
D. The id of the author

Solution

  1. Step 1: Understand ForeignKey access in Django

    A ForeignKey field allows access to the related model instance. So book.author gives the Author instance linked to the book.
  2. Step 2: Access the related model's attribute

    Using book.author.name accesses the name attribute of the related Author instance, returning the author's name.
  3. Final Answer:

    The name of the author related to the book -> Option A
  4. Quick Check:

    ForeignKey attribute access returns related model data [OK]
Hint: ForeignKey lets you access related object attributes directly [OK]
Common Mistakes:
  • Expecting book.author to be just an ID
  • Thinking book.author.name causes error
  • Confusing book.title with author name
4. Identify the error in this Django model relationship:
class Profile(models.Model):
    user = models.OneToOneField(User)
    bio = models.TextField()
medium
A. OneToOneField should be ForeignKey
B. Missing on_delete argument in OneToOneField
C. TextField cannot be used for bio
D. User model must be imported

Solution

  1. Step 1: Check OneToOneField required arguments

    Django requires the on_delete argument for relationship fields like OneToOneField to specify behavior on deletion.
  2. Step 2: Identify missing argument

    The model misses on_delete=models.CASCADE or similar, which causes an error.
  3. Final Answer:

    Missing on_delete argument in OneToOneField -> Option B
  4. Quick Check:

    Relationship fields need on_delete argument [OK]
Hint: Always add on_delete to OneToOneField and ForeignKey [OK]
Common Mistakes:
  • Omitting on_delete argument
  • Confusing OneToOneField with ForeignKey
  • Assuming TextField is invalid for bio
5. You want to model a social app where users can follow many other users and be followed by many users. Which Django model field setup correctly represents this many-to-many self-relationship?
hard
A. followers = models.OneToOneField('self', on_delete=models.CASCADE)
B. followers = models.ForeignKey('self', on_delete=models.CASCADE)
C. followers = models.ManyToManyField('self', symmetrical=False, related_name='following')
D. followers = models.ManyToManyField(User)

Solution

  1. Step 1: Understand many-to-many self-relationship

    Users can follow many users and be followed by many, so a many-to-many relationship to self is needed.
  2. Step 2: Check correct ManyToManyField syntax for self

    Using ManyToManyField('self', symmetrical=False, related_name='following') correctly models directed follow relationships.
  3. Final Answer:

    followers = models.ManyToManyField('self', symmetrical=False, related_name='following') -> Option C
  4. Quick Check:

    ManyToManyField to 'self' with symmetrical=False models follow [OK]
Hint: Use ManyToManyField('self', symmetrical=False) for directed self-relations [OK]
Common Mistakes:
  • Using ForeignKey or OneToOneField for many-to-many
  • Omitting symmetrical=False for directed relation
  • Using ManyToManyField(User) instead of 'self'
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard