Ticket #11256: 11256_r13984.diff

File 11256_r13984.diff, 1.9 KB (added by Carl Meyer, 14 years ago)

updated patch

  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index e16fca1..37096ba 100644
    a b class QuerySet(object):  
    619619        with data aggregated from related fields.
    620620        """
    621621        for arg in args:
     622            if arg.default_alias in kwargs:
     623                raise ValueError("The %s named annotation conflicts with the "
     624                                 "default name for another annotation."
     625                                 % arg.default_alias)
    622626            kwargs[arg.default_alias] = arg
    623627
     628        names = set([f.name for f in self.model._meta.fields])
     629        for aggregate in kwargs:
     630            if aggregate in names:
     631                raise ValueError("The %s annotation conflicts with a field on "
     632                    "the model." % aggregate)
     633
     634
    624635        obj = self._clone()
    625636
    626637        obj._setup_aggregate_query(kwargs.keys())
  • tests/regressiontests/aggregation_regress/tests.py

    diff --git a/tests/regressiontests/aggregation_regress/tests.py b/tests/regressiontests/aggregation_regress/tests.py
    index 51f439c..7864314 100644
    a b class AggregationTests(TestCase):  
    481481            lambda b: b.name
    482482        )
    483483
     484    def test_duplicate_alias(self):
     485        # Regression for #11256 - duplicating a default alias raises ValueError.
     486        self.assertRaises(ValueError, Book.objects.all().annotate, Avg('authors__age'), authors__age__avg=Avg('authors__age'))
     487
     488    def test_field_name_conflict(self):
     489        # Regression for #11256 - providing an aggregate name that conflicts with a field name on the model raises ValueError
     490        self.assertRaises(ValueError, Author.objects.annotate, age=Avg('friends__age'))
     491
    484492    def test_pickle(self):
    485493        # Regression for #10197 -- Queries with aggregates can be pickled.
    486494        # First check that pickling is possible at all. No crash = success
Back to Top