📊

Aggregation and Annotation in Django ORM

May 15, 2025

Lecture Notes: Aggregation and Annotation in Django ORM

Introduction

  • Aggregating data is crucial for gaining insights into large datasets.
  • Commonly used in dashboards, reporting applications, and business intelligence software.
  • Focus on extracting insights from whole datasets rather than individual rows.

Aggregating and Annotating Data

  • Aggregation: Combine multiple row values into a single output (e.g., sum, max, min).
  • Annotation: Add summary records or aggregate values for each model in a queryset.

Key Functions

  • Values and Values List Functions:
    • values(): Returns dictionaries, allowing selection of specific columns.
    • values_list(): Returns tuples, often more performant.
    • Used to fetch subsets of columns rather than all columns.

Practical Usage

  • Using values()
    • Fetch specific fields, improving performance and efficiency.
    • Example: Only fetch the name and date opened of restaurants.
  • Using values_list()
    • Returns tuples of field values.
    • flat=True: Flattens the output to a list without tuples.

Aggregation in Django

  • Basic Aggregations:
    • count(), avg(), min(), max(), sum().
    • aggregate(): Allows multiple aggregations, returns a dictionary.
  • Filtered Aggregations:
    • Example: Filter sales by date (last month) before aggregation.

Annotation in Django

  • Adding Annotations:
    • annotate(): Adds computed fields to each model in a queryset.
    • Example: Annotate restaurants with the length of their name.
  • Using Database Functions:
    • Functions like Upper and Concat are used for transformations.

Grouping in Annotations

  • Grouping with values() before annotate():
    • Groups records before applying annotations.
    • Example: Aggregate ratings by restaurant type rather than individual restaurants.

Order and Filter by Annotations

  • Ordering and Filtering:
    • Use filter() and order_by() on annotated fields.
    • Example: Order restaurants by total sales, filter those with sales < 300.

Conclusion

  • Django ORM provides powerful tools for data aggregation and annotation.
  • Enables efficient querying and data insight extraction.
  • Next steps: Advanced querying operations in Django.