Complete the code to define a Django model with a foreign key relationship.
class Book(models.Model): author = models.ForeignKey([1], on_delete=models.CASCADE) title = models.CharField(max_length=100)
The ForeignKey field links the Book model to the Author model, showing a real-world relationship where a book has an author.
Complete the code to add a many-to-many relationship between students and courses.
class Student(models.Model): courses = models.[1](Course) name = models.CharField(max_length=50)
ManyToManyField is used to model a many-to-many relationship, like students enrolled in multiple courses.
Fix the error in the model relationship by completing the code.
class Profile(models.Model): user = models.OneToOneField([1], on_delete=models.CASCADE) bio = models.TextField()
The OneToOneField should link to the User model to represent a one-to-one relationship between a profile and a user.
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.
course_counts = {course.name: [1] for course in courses if [2] > 5}Use course.students.count() to get the number of students enrolled in each course and filter courses with more than 5 students.
Fill all three blanks to create a queryset filtering authors who have written more than 3 books and order them by name.
authors = Author.objects.annotate(book_count=[1]).filter([2]__gt=[3]).order_by('name')
Use Count('book') to annotate the number of books per author, filter authors with book_count > 3, and order by name.