聚合函数返回 AttributeError: 'Sum'对象没有属性“查找”;
我正在尝试聚合函数,并且得到了这个奇怪的结果(最新的官方 Django 1.2 版本)。 这是模型:
class Reputation(models.Model):
user = models.ForeignKey(User)
modifier = models.IntegerField()
activity = models.ForeignKey(Activity)
这就是我得到的:
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'
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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
确保导入正确的
Sum
:如果导入
django.db.models.sql.aggregates.Sum
,您将继续看到该错误。Make sure you import the correct
Sum
:If you import
django.db.models.sql.aggregates.Sum
, you will continue to see that error.从您的帖子是通过谷歌搜索唯一提到该错误消息的事实来看,我可以通过在聚合函数中使用名为
Sum
的随机类来重现您的错误,我认为您有一个代码中Sum
的本地定义。您位于控制台会话的第 37 行。尝试重新开始,
from django.db.models import Sum
,from myproject.myapp.models import Reputation
,然后是查询。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 ofSum
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.我由于未能在注释中包含聚合函数而遇到了同样的错误,即
错误:
正确:
我的一个愚蠢的错误,但我想我会在这里记录它,以防它对其他人有帮助。
I got the same error by failing to include the aggregation function within an annotation i.e.
wrong:
right:
A stupid mistake on my part, but thought I'd document it here in case it helps anyone else.
这并不完全是问题所在,但我在
annotate
中使用F
遇到了几乎相同的错误(没有属性“lookup”)。导入是正确的,但其在注释中的使用
是在 Django 1.8 中添加的,而我正在使用 Django 1.7。您需要使用 <如果您还使用旧的 Django 版本,请改为使用 code>filter 。This is not exactly what the question is about, but I encountered almost the same error (no attribute 'lookup') using
F
inannotate
. The import was correct, but its use inannotate
was added in Django 1.8 whilst I am using Django 1.7. You’d need to usefilter
instead if you’re also using that old Django release.