Ticket #11256: aggregation-validationl.diff

File aggregation-validationl.diff, 1.9 KB (added by Alex Gaynor, 15 years ago)
  • django/db/models/query.py

    diff --git a/django/db/models/query.py b/django/db/models/query.py
    index 0d35b0b..63e000d 100644
    a b class QuerySet(object):  
    536536        Return a query set in which the returned objects have been annotated
    537537        with data aggregated from related fields.
    538538        """
     539        total_aggregates = len(args) + len(kwargs)
    539540        for arg in args:
    540541            kwargs[arg.default_alias] = arg
    541542
     543        if len(kwargs) != total_aggregates:
     544            raise ValueError("Some of the aggregates had duplicate aliases, "
     545                "check to make sure none of your aggregates conflict with the "
     546                "default aliases.")
     547
     548        names = set([f.name for f in self.model._meta.fields])
     549        for aggregate in kwargs:
     550            if aggregate in names:
     551                raise ValueError("The %s aggregate conflicts with a field on "
     552                    "the model." % aggregate)
     553
     554
    542555        obj = self._clone()
    543556
    544557        obj._setup_aggregate_query(kwargs.keys())
  • tests/regressiontests/aggregation_regress/models.py

    diff --git a/tests/regressiontests/aggregation_regress/models.py b/tests/regressiontests/aggregation_regress/models.py
    index 4476b86..857890c 100644
    a b Traceback (most recent call last):  
    318318...
    319319FieldError: Cannot compute Avg('mean_age'): 'mean_age' is an aggregate
    320320
     321>>> Book.objects.all().annotate(Avg('authors__age'), authors__age__avg=Avg('authors__age'))
     322Traceback (most recent call last):
     323...
     324ValueError: Some of the aggregates had duplicate aliases, check to make sure none of your aggregates conflict with the default aliases.
     325
     326>>> Author.objects.annotate(age=Avg('friends__age'))
     327Traceback (most recent call last):
     328...
     329ValueError: The age aggregate conflicts with a field on the model.
     330
    321331"""
    322332}
    323333
Back to Top