Google Groups

Re: Custom Aggregate Objects


Alex_Gaynor Feb 2, 2009 6:16 PM
Posted in group: Django users


On Feb 2, 9:05 pm, nsitarz <nsit...@gmail.com> wrote:
> Hey,
>
> Back when the ORM aggregate support was a patch in trac I used to
> create custom aggregate objects that looked like this:
>
> class Date(Aggregate):
>     pass
>
> class DateHour(Aggregate):
>     def __init__(self, lookup):
>         super(DateHour, self).__init__(lookup)
>         self.sql_template = '(DATE(${field}) + INTERVAL HOUR(${field})
> HOUR)'
>
> However now that aggregate support has made its way into trunk the api
> seems to have changed a bit. After reading the aggregate module it
> looks to me like I'd have to reimplement the Aggregate class myself in
> order to have this same kind of functionality. Is there a supported
> way of creating custom aggregate classes for use with the trunk ORM?
>
> I'd appreciate any help that anybody can offer on this.

Aggregates were refactored so that they involve 2 classes, 1
implements the public API, and the other implements the SQL bit.
Basically look here:
http://code.djangoproject.com/browser/django/trunk/django/db/models/aggregates.py#L26

for what you're public API bit should be(instead of pulling the clsas
out of the aggreagetes module it can just refer to te specific class
it goes with).  And then here:
http://code.djangoproject.com/browser/django/trunk/django/db/models/sql/aggregates.py
for what the SQL backened's API is.

Alex