Ticket #10929: 10929-aggregates-default-r17029.diff
File 10929-aggregates-default-r17029.diff, 2.2 KB (added by , 13 years ago) |
---|
-
tests/modeltests/aggregation/tests.py
565 565 (Decimal('82.8'), 1), 566 566 ] 567 567 ) 568 569 def test_default(self): 570 p = Publisher.objects.create(name='publisher', num_awards=0) 571 # return None if there's no default and no data to aggregate. 572 publisher = Publisher.objects.annotate(avg_book_rating=Avg("book__rating")).get(pk=p.pk) 573 self.assertEqual(publisher.avg_book_rating, None) 574 # return the default if one is specified and no data to aggregate. 575 publisher = Publisher.objects.annotate(avg_book_rating=Avg("book__rating", 0)).get(pk=p.pk) 576 self.assertEqual(publisher.avg_book_rating, 0.0) -
django/db/models/sql/aggregates.py
89 89 } 90 90 params.update(self.extra) 91 91 92 return self.sql_template % params 92 template = self.sql_template 93 if 'default' in params: 94 template = 'COALESCE(%s, %%(default)s)' % template 93 95 96 return template % params 94 97 98 95 99 class Avg(Aggregate): 96 100 is_computed = True 97 101 sql_function = 'AVG' -
django/db/models/aggregates.py
6 6 """ 7 7 Default Aggregate definition. 8 8 """ 9 def __init__(self, lookup, **extra):9 def __init__(self, lookup, default=None, **extra): 10 10 """Instantiate a new aggregate. 11 11 12 12 * lookup is the field on which the aggregate operates. … … 16 16 Also utilizes the class variables: 17 17 * name, the identifier for this aggregate function. 18 18 """ 19 if default is not None: 20 extra.setdefault('default', default) 19 21 self.lookup = lookup 20 22 self.extra = extra 21 23