Django- 'WhereNode'对象没有属性 'output_field'错误

标签 django django-models django-2.0 django-aggregation django-annotate

我正在尝试查询和注释模型中的一些数据:

class Feed(models.Model):     # Feed of content
    user = models.ForeignKey(User, on_delete=models.CASCADE)

class Piece(models.Model):    # Piece of content (video or playlist)
    removed = models.BooleanField(default=False)
    feed    = models.ForeignKey(Feed, on_delete=models.CASCADE)
    user    = models.ForeignKey(User, on_delete=models.CASCADE)

在以下查询中未使用其他字段,因此在此跳过了它们。

在我看来,我需要获取经过身份验证的用户的所有供稿的queryset。批注应包含未删除的所有部分的数量。

最初,Piece模型不包含removed字段,并且一切都可以与queryset一起运行,如下所示:
Feed.objects.filter(user=self.request.user).annotate(Count('piece'))

但是随后我将removed字段添加到Piece模型中,只需要计算未删除的部分:
Feed.objects.filter(user=self.request.user)
            .annotate(Count('piece'), filter=Q(piece__removed=False))

它给了我以下错误:
'WhereNode' object has no attribute 'output_field'

这只是django在错误页面上输出的内容的一小部分,因此,如果这还不够的话,请告诉我我需要在问题中包含的内容。

我试图在此处到处包括output_fieldmodels.IntegerField()models.FloatField()(已正确导入)等选项,但出现了一些我未在此处提供的错误,因为我认为这些操作没有任何意义。

我正在使用Django 2.0.3

最佳答案

您的语法错误在这里,

Feed.objects.filter(user=self.request.user)
            .annotate(Count('piece', filter=Q(piece__removed=False)))

过滤器需要应用Count而不是annotate

Django文档中的引用:
https://docs.djangoproject.com/en/2.1/topics/db/aggregation/#filtering-on-annotations

关于Django- 'WhereNode'对象没有属性 'output_field'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49643502/

相关文章:

Django key 错误

Django:在创建对象时搜索多对多字段

python - 如何在Django2网址中以斜线传递参数

python - django login_required 装饰器,即使在登录后也总是重定向到登录页面

python - Django 如何创建表(sqlite)?

python-3.x - 如何在 Django 中正确创建 InMemoryUploadedFile 对象

django - 多对多关系查询在 post_save 信号上返回空查询集,但在 django shell 中不返回

python - 如何在 django html 中导入模块?

html - 如何在 Django 中将 CSS 链接到 HTML?

python - 列 <column> 不存在(Django 1.8)