My favorites | Sign in
Project Logo
                
Search
for
Updated May 28, 2008 by nicolaslara
DesignDecisions  

#Design decisions on aggregate/annotate behaviour

Introduction

Here is a list of desing decisions that have been taken on the behaviour of aggregates by the comunity (for big decisions) or by me (for small decisions). If you have any questions/sugestion, please post on django-dev or contact me directly.

Design Decisions

In words everybody understands:
#In the following example the author who has no friends, Brad Dayley (My apologies to Brad Dayley, the choice of friends was completly random), will not appear in the results. 
>>> authors = Author.objects.all().annotate(Avg('friends__age')).order_by('id')
>>> len(authors)
8
>>> for i in authors:
...     print i.name, i.friends__age__avg
...
Adrian Holovaty 32.0
Jacob Kaplan-Moss 29.5
James Bennett 34.0
Jeffrey Forcier  27.0
Paul Bissex 31.0
Wesley J. Chun 33.6666666667
Peter Norvig 46.0
Stuart Russell 57.0

#To include every object retrieved by the previous query, Author.objects.all(), whether they have a m2m relationship or not we use the allow_nulls parameter.
#Notice that this affects the performance at the database level so try to avoid it if possible

>>> authors = Author.objects.all().annotate(Avg('friends__age'), allow_nulls=True).order_by('id')
>>> len(authors)
9
>>> for i in authors:
...     print i.name, i.friends__age__avg
...
Adrian Holovaty 32.0
Jacob Kaplan-Moss 29.5
Brad Dayley None
James Bennett 34.0
Jeffrey Forcier  27.0
Paul Bissex 31.0
Wesley J. Chun 33.6666666667
Peter Norvig 46.0
Stuart Russell 57.0
This decision has changed. Now Joins are always promoted. In the future a better analysis should be made to decide which joins to promote if possible.

Sign in to add a comment
Hosted by Google Code