0
0
Djangoframework~10 mins

Related name for reverse access 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 define a ForeignKey with a related name for reverse access.

Django
class Book(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name=[1])
Drag options to blanks, or click blank then click option'
A"author"
B"books"
C"book_author"
D"authors"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the model name 'author' as related_name causes confusion.
Omitting quotes around the related_name string.
2fill in blank
medium

Complete the code to access all books of an author using the related name.

Django
author = Author.objects.get(id=1)
books = author.[1].all()
Drag options to blanks, or click blank then click option'
Abooks
Bbook_set
Cauthor_books
Dbook
Attempts:
3 left
💡 Hint
Common Mistakes
Using the default book_set when a related_name is set.
Using singular form which is incorrect for reverse access.
3fill in blank
hard

Fix the error in the ForeignKey definition to correctly set the related name.

Django
class Comment(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name=[1])
Drag options to blanks, or click blank then click option'
A"comments"
B"comment"
C"post_comments"
D"post"
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular related_name causing confusion in reverse queries.
Using the model name 'post' which conflicts with the ForeignKey field name.
4fill in blank
hard

Fill both blanks to define a ForeignKey with a related name and access the related objects.

Django
class Entry(models.Model):
    blog = models.ForeignKey(Blog, on_delete=models.CASCADE, related_name=[1])

entries = blog.[2].all()
Drag options to blanks, or click blank then click option'
A"entries"
B"entry_set"
Centries
D"blogs"
Attempts:
3 left
💡 Hint
Common Mistakes
Using default entry_set when related_name is set.
Mismatch between related_name and access attribute.
5fill in blank
hard

Fill all three blanks to define a ForeignKey with a related name, access related objects, and filter them.

Django
class Review(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name=[1])

reviews = product.[2].filter(rating__[3]=5)
Drag options to blanks, or click blank then click option'
A"reviews"
Breviews
Cgte
Dlte
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong related_name causing attribute errors.
Using incorrect filter lookup like 'lte' instead of 'gte'.