Home > Programming, Python > Aggregating data collections made simple in Django 1.1

Aggregating data collections made simple in Django 1.1

February 24th, 2009

Aggregation, the new addition to Django QuerySet in version 1.1, makes it really simple to summarizing data collections.

I am currently working on a site that will provide free online petition hosting with a key focus on the South African public.
I decided to have an embarrassingly simple “Top 10″ page on the site that will show currently popular petitions. The new Django aggregation support makes this task simple.

Requirement:
List top 10 petitions grouping them by “total number of signatures” and “newest signature, published date” per petition.
Order petitions in descending order by “total number of signatures” and “newest signature, published date”.

Simple solution using the new Django annotate() clause:

top_petitions = Petition.objects.annotate(
    Count("petitionsignature"),
    Max("petitionsignature__pub_date")
).order_by("-petitionsignature__count",
    "-petitionsignature__pub_date__max")[:10]

>>> top_petitions[0].petitionsignature__count
5
>>> top_petitions[0].petitionsignature__pub_date__max
datetime.datetime(2009, 2, 15, 18, 55, 23, 263325)

Both the above attributes, petitionsignature__count and petitionsignature__pub_date__max are name identifiers for the aggregate values. By default, the name is automatically generated from the name of the field and the aggregate function name separating the two with a double underscore. You can manually specify the name for the aggregate value by defining it directly in the aggregate or annotate clause.

For more information on Django aggregation support, you can read the official guide here: http://docs.djangoproject.com/en/dev/topics/db/aggregation/

alen Programming, Python , ,

  1. No comments yet.
  1. No trackbacks yet.