【发布时间】:2021-11-03 14:22:51
【问题描述】:
Django 注释真的很棒。但是我不知道如何处理需要多个 values() 的注释。
问题:
我想用相关 m2m 中的项目数注释 author_queryset。我不知道我是否需要使用Subquery,但是:
annotated_queryset = author_queryset.annotate(genre_counts=Subquery(genre_counts))
返回:
SyntaxError: subquery must return only one column
我已尝试将值转换为 JSONField 以将其返回到一个字段中,希望我可以在其上使用 JSONBagg,因为我使用的是 postgres 并且需要过滤结果:
subquery = Author.objects.filter(id=OuterRef('pk')).values('id','main_books__genre').annotate(genre_counts=Count('main_books__genre'))
qss = qs.annotate(genre_counts=Subquery(Cast(subquery,JSONField()), output_field=JSONField()))
产量:
AttributeError: 'Cast' object has no attribute 'clone'
我不确定需要什么才能将 dict 转换为 JSONField()。有一些很棒的info here 关于过滤这些。在名为ArraySubQuery() 的开发版本中,postgres 也即将推出一些东西,旨在解决这个问题。但是,在它处于稳定版本之前,我无法使用此功能。
想要的结果
我想进行注释,以便可以根据注释进行过滤,如下所示:
annotated_queryset.filter(genre_counts__scifi__gte=5)
详情
我可以使用 dunders 来获取相关领域,然后像这样计算:
# get all the authors with Virginia in their name
author_queryset = Author.objects.filter(name__icontains='Virginia')
author_queryset.count()
# returns: 21
# aggregate the book counts by genre in the Book m2m model
genre_counts = author_queryset.values('id','main_books__genre').annotate(genre_counts=Count('main_books__genre'))
genre_counts.count()
# returns: 25
这是因为查询集中的每个 Author 对象可以返回多个类型计数。在这个特定的示例中,有一个作者拥有 4 种不同类型的书籍:
举例说明:
...
{'id': 'authorid:0054f04', 'main_books__genre': 'scifi', 'genre_counts': 1}
{'id': 'authorid:c245457', 'main_books__genre': 'fantasy', 'genre_counts': 4}
{'id': 'authorid:a129a73', 'main_books__genre': None, 'genre_counts': 0}
{'id': 'authorid:f41f14b', 'main_books__genre': 'mystery', 'genre_counts': 16}
{'id': 'authorid:f41f14b', 'main_books__genre': 'romance', 'genre_counts': 1}
{'id': 'authorid:f41f14b', 'main_books__genre': 'scifi', 'genre_counts': 9}
{'id': 'authorid:f41f14b', 'main_books__genre': 'fantasy', 'genre_counts': 3}
...
还有一个作者有 2 个,其余的每个都有一个流派。这是 25 个值。
希望这对某人有意义!我确信有一种方法可以正确处理此问题,而无需等待上述功能。
【问题讨论】:
标签: python django postgresql django-models django-queryset