2

I'm trying out the aggregation functions, and I get this strange results (latest official Django 1.2 release). Here's the model:

class Reputation(models.Model):
    user = models.ForeignKey(User)
    modifier = models.IntegerField()
    activity = models.ForeignKey(Activity)

This is what I get:

In [37]: Reputation.objects.aggregate(r=Sum('modifier'))
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)

C:\Data\Development\django_projects\oko\lib\site-packages\django\db\models\manager.pyc in aggregate(self, *args, **kwargs)
    142
    143     def aggregate(self, *args, **kwargs):
--> 144         return self.get_query_set().aggregate(*args, **kwargs)
    145
    146     def annotate(self, *args, **kwargs):

C:\Data\Development\django_projects\oko\lib\site-packages\django\db\models\query.pyc in aggregate(self, *args, **kwargs)
    315         for (alias, aggregate_expr) in kwargs.items():
    316             query.add_aggregate(aggregate_expr, self.model, alias,
--> 317                 is_summary=True)
    318
    319         return query.get_aggregation(using=self.db)

C:\Data\Development\django_projects\oko\lib\site-packages\django\db\models\sql\query.pyc in add_aggregate(self, aggregate, model, alias, is_summary)
    929         """
    930         opts = model._meta
--> 931         field_list = aggregate.lookup.split(LOOKUP_SEP)
    932         if len(field_list) == 1 and aggregate.lookup in self.aggregates:
    933             # Aggregate is over an annotation

AttributeError: 'Sum' object has no attribute 'lookup'
Mathieu Dhondt
  • 8,405
  • 5
  • 37
  • 58

4 Answers4

6

Make sure you import the correct Sum:

from django.db.models import Sum

If you import django.db.models.sql.aggregates.Sum, you will continue to see that error.

4

Judging by the fact that your post is the sole mention of that error message via google searching, and that I can reproduce your error by using a random class called Sum into an aggregation function, I think you have a local definition of Sum in your code.

You're on line 37 of your console session. Try starting anew, from django.db.models import Sum, from myproject.myapp.models import Reputation, then the query.

Yuji 'Tomita' Tomita
  • 115,817
  • 29
  • 282
  • 245
1

I got the same error by failing to include the aggregation function within an annotation i.e.

wrong:

Person.objects.annotate(ave_score='starred_in__score')

right:

Person.objects.annotate(ave_score=Avg('starred_in__score'))

A stupid mistake on my part, but thought I'd document it here in case it helps anyone else.

Liz Rice
  • 589
  • 6
  • 9
1

This is not exactly what the question is about, but I encountered almost the same error (no attribute 'lookup') using F in annotate. The import was correct, but its use in annotate was added in Django 1.8 whilst I am using Django 1.7. You’d need to use filter instead if you’re also using that old Django release.

Yushin Washio
  • 675
  • 8
  • 12