ExamHoot

Django Model Aggregation and Annotation

from django.db.models import Count, Avg, Q

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    rating = models.IntegerField()
    pages = models.IntegerField()

# Prasath wants to find authors with more than 5 books and average rating > 4:
result = Author.objects.annotate(
    book_count=Count('book'),
    avg_rating=Avg('book__rating')
).filter(book_count__gt=5, avg_rating__gt=4)

# Prasath also wants to get the total pages across all books:
total = Book.objects.aggregate(total_pages=Sum('pages'))
  1. The code will fail because aggregate() cannot be used after annotation in the same queryset.

  2. The code is correct: annotate() adds book_count and avg_rating to each Author object, filter() narrows results, and aggregate() computes total_pages across all books.

  3. The aggregate() call should use annotate() instead to calculate total pages per author.

  4. The filter() condition will not work because avg_rating is not a database field.

Show answer & explanation

Correct answer

The code is correct: annotate() adds book_count and avg_rating to each Author object, filter() narrows results, and aggregate() computes total_pages across all books.

Explanation

Annotation adds calculated fields to each object in the queryset, while aggregation returns a single dictionary with aggregate values across the entire queryset. The query correctly uses annotate() for per-author calculations and aggregate() for global totals.

Written by ExamHoot EditorialPublished · Updated

All 30 Django Models and Database Design questions

  1. 1.What is the primary purpose of Django Models?
  2. 2.Which field type should be used to store an email address in a Django Model?
  3. 3.What does the Meta class inside a Django Model do?
  4. 4.Which relationship type is used when one model can have many instances of another model?
  5. 5.What is the purpose of the `__str__` method in a Django Model?
  6. 6.Which field attribute makes a field mandatory in Django Models?
  7. 7.What does the `on_delete` parameter do in a ForeignKey relationship?
  8. 8.Which of the following is a valid Django field type for storing boolean values?
  9. 9.What is the default behavior when you don't specify `null=True` for a field in Django?
  10. 10.Which method is used to retrieve a single object from the database in Django?
  11. 11.Understanding Django Model Meta Options
  12. 12.Scenario:
  13. 13.Django QuerySet Filter vs Get Method
  14. 14.Coding:
  15. 15.Scenario:
  16. 16.Understanding Django Model Inheritance
  17. 17.Coding:
  18. 18.Scenario:
  19. 19.Django Model Manager and QuerySet Methods
  20. 20.Coding:
  21. 21.Django Model Inheritance Strategy Selection
  22. 22.Django QuerySet Optimization with select_related vs prefetch_related
  23. 23.Django Model Field Choices and Validation
  24. 24.Django Model Relationships:
  25. 25.Django Model Signals and Database Consistency
  26. 26.Django Model Meta Options:
  27. 27.Django Model Managers and Custom QuerySets
  28. 28.Django Model Field:
  29. 29.Django Model Validation:
  30. 30.Django Model Aggregation and Annotation