|
1 | | -from django.db.models.sql.aggregates import * |
| 1 | +from django.db.models.sql.aggregates import Aggregate |
2 | 2 |
|
3 | | -class StdDev(Aggregate): |
| 3 | +class _Aggregate(Aggregate): |
| 4 | + |
| 5 | + def __init__(self, lookup, **extra): |
| 6 | + self.lookup = lookup |
| 7 | + self.extra = extra |
| 8 | + |
| 9 | + def _default_alias(self): |
| 10 | + return '%s__%s' % (self.lookup, self.__class__.__name__.lower()) |
| 11 | + default_alias = property(_default_alias) |
| 12 | + |
| 13 | + def add_to_query(self, query, alias, col, source, is_summary): |
| 14 | + super(_Aggregate, self).__init__(col, source, is_summary, **self.extra) |
| 15 | + query.aggregates[alias] = self |
| 16 | + |
| 17 | +class StdDev(_Aggregate): |
| 18 | + name = 'StdDev' |
4 | 19 | is_computed = True |
5 | 20 |
|
6 | 21 | def __init__(self, col, sample=False, **extra): |
7 | 22 | super(StdDev, self).__init__(col, **extra) |
8 | 23 | self.sql_function = sample and 'STDEV' or 'STDEVP' |
9 | 24 |
|
10 | | -class Variance(Aggregate): |
| 25 | +class Variance(_Aggregate): |
| 26 | + name = 'Variance' |
11 | 27 | is_computed = True |
12 | 28 |
|
13 | 29 | def __init__(self, col, sample=False, **extra): |
14 | 30 | super(Variance, self).__init__(col, **extra) |
15 | 31 | self.sql_function = sample and 'VAR' or 'VARP' |
16 | 32 |
|
17 | | -class Avg(Aggregate): |
| 33 | +class Avg(_Aggregate): |
| 34 | + name = 'Avg' |
18 | 35 | is_computed = True |
19 | 36 | sql_function = 'AVG' |
20 | 37 | sql_template = '%(function)s(Convert(FLOAT, %(field)s))' |
0 commit comments