Add mention of backward relationship in aggregate docs. #714

Closed
wants to merge 7 commits into
from

Projects

None yet

3 participants

@ramiro
Member
ramiro commented Feb 11, 2013

This is for ticket 19803 (https://code.djangoproject.com/ticket/19803).

@akaariai akaariai and 1 other commented on an outdated diff Feb 11, 2013
docs/topics/db/aggregation.txt
+
+This doesn't apply just to foreign keys. It also works with many-to-many
+relations. For example, we can ask for every author, annotated with the total
+number of pages considering all the books he/she has (co-)authored (note how we
+use `'book'` to specify the Author->Book reverse many-to-many hop)::
+
+ >>> Author.objects.annotate(Sum('book__pages'))
+
+(Every Author in the resulting QuerySet will have an extra ``book__pages__sum``
+attribute.)
+
+Or ask for the author(s) of the best-ranked book::
+
+ >>> Author.objects.aggregate(Max('book__rating'))
+
+(The resulting dictionary wil have a `'book__rating__max'` key.)
@akaariai
akaariai Feb 11, 2013 Member

wil -> will

@ramiro
ramiro Feb 11, 2013 Member

Thanks Anssi.

@akaariai
Member

Some of the examples could use .annotate(somevar=Count('long__traversal__here')) style just to show that is also possible. Code is usually more readable when that is done.

A warning: if you traverse two different reverse relations it is likely that the results of the query will be wrong. See: https://code.djangoproject.com/ticket/10060. I don't know if it is worth mentioning in the docs, this is a clear bug in the aggregation code...

@mjtamlyn

Can we have a nicer named variable here? Like book_count or total_books?

Owner

book_cnt is too similar to book__cnt and total_books say even less about the real meaning. Maybe we should go the full name way after all by simply use publisher_books_count ?

Yeah it's verbose but obvious. Alternatively maybe we should leave this one as default and only override them when the name isn't descriptive, like in the book__pubdate__min example below?

The main thing is to avoid cnt as an abbreviation, it looks much more like something other than count........

Owner

Yes Sir. You are completely right Thanks!

@ramiro
Member
ramiro commented Feb 12, 2013

Thans @akaariai for remind me about #10060. I will remove the two last examples for now.

@ramiro
Member
ramiro commented Feb 12, 2013

Applied in 0560bfb.

@ramiro ramiro closed this Feb 12, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment