聚合函数返回 AttributeError: 'Sum'对象没有属性“查找”;

发布于 2024-10-23 00:47:10 字数 1630 浏览 4 评论 0原文

我正在尝试聚合函数,并且得到了这个奇怪的结果(最新的官方 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唯憾梦倾城 2024-10-30 00:47:10

确保导入正确的 Sum

from django.db.models import Sum

如果导入 django.db.models.sql.aggregates.Sum,您将继续看到该错误。

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.

小兔几 2024-10-30 00:47:10

从您的帖子是通过谷歌搜索唯一提到该错误消息的事实来看,我可以通过在聚合函数中使用名为 Sum 的随机类来重现您的错误,我认为您有一个代码中 Sum 的本地定义。

您位于控制台会话的第 37 行。尝试重新开始,from django.db.models import Sumfrom 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 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.

橪书 2024-10-30 00:47:10

我由于未能在注释中包含聚合函数而遇到了同样的错误,即

错误:

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

正确:

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

我的一个愚蠢的错误,但我想我会在这里记录它,以防它对其他人有帮助。

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.

懒的傷心 2024-10-30 00:47:10

这并不完全是问题所在,但我在 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文