Hello,
Here are some updates on what's going on with aggregate support and
some questions.
The latest addition to aggregates is the possibility to aggregate on a
set that already have annotations. This has been discussed before in
[1] and [2]. So far it is implemented in a way that you can obtain
"the average number of books per author and the maximum author age"
like this:
>>> Author.objects.filter(age__gt=25).annotate(num_books=Count('book')).aggregate(Avg('num_books'),
>>> Max('age'))
{'num_books__avg': 1.125, 'age__max': 57.0}
Also I have patched the branch with the patch for #7210 but am still
having problems using F-Syntax with aggregates.
The test case I am using as a base is something like:
>>> Author.objects.all().annotate(num_coleagues=Count('book__authors__id'),
>>> num_books=Count('book__id', distinct=True)).filter(num_coleagues=F('id'))
This currently generates the expected SQL (I know the sql is a bit
unreadable, but I post it here for completeness):
('SELECT "modeltests_author"."id", "modeltests_author"."name",
"modeltests_author"."age", COUNT(T5."id") AS num_coleagues,
COUNT(DISTINCT "modeltests_book"."id") AS num_books FROM
"modeltests_author" LEFT OUTER JOIN "modeltests_book_authors" ON
("modeltests_author"."id" = "modeltests_book_authors"."author_id")
LEFT OUTER JOIN "modeltests_book" ON
("modeltests_book_authors"."book_id" = "modeltests_book"."id") LEFT
OUTER JOIN "modeltests_book_authors" T4 ON ("modeltests_book"."id" =
T4."book_id") LEFT OUTER JOIN "modeltests_author" T5 ON
(T4."author_id" = T5."id") GROUP BY "modeltests_author"."id",
"modeltests_author"."name", "modeltests_author"."age" HAVING
COUNT(T5."id") = %s',
('"modeltests_author"."id"',))
The only problem is that when trying to execute that query via
"cursor.execute()" it fails tu include the string parameter in the
query. It fails with:
ProgrammingError: invalid input syntax for integer:
""modeltests_author"."id""
At the moment I haven't been able to figure out what is going wrong. I
have made it work by inserting the value in the query instead of
returning it as a parameter, but seems to be the wrong way to do it.
I'll look deeper into this and I expect to have F-syntax working with
aggregates by the end of next week at latest.
Also there are some tasks missing with the F-Syntax patch that I
intend to fill as I go through with it. These include extending and
organizing the tests and (maybe) try to extend it to span relations.
Any help regarding my current error would be great =)
Last week I presented the task of creating custom aggregate objects as
one of the next tasks for this week but I let it behing F-syntax
because I believe it is less important and would require some
discussion before deciding a syntax. The current implementation is
enough to create custom aggregate objects that receive only one
columns as parameter by subclassing.
Regards =)
Nicolas
[1]
http://groups.google.com/group/django-developers/browse_thread/thread/f83457fc3cdb235d/ae3e623ff8da990e
[2]
http://groups.google.at/group/django-developers/browse_thread/thread/8ceeb0c4a5ca441d/a1c03a002c5fb89e
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---