给出以下模型,我想获取所有页面的查询集,并用与该页面关联的线程中的注释总数进行注释,包括与页面关联的注释线程树中的所有注释。
我正在使用django-mptt存储评论树。
我可以使用comment.get_descendant_count()
在python中获取此信息,但这在查询所有页面时效率很低
class CommentThread(models.Model):
...
class Page(models.Model):
...
thread = models.ForeignKey("CommentThread", ...)
class Comment(MPTTModel):
body = models.TextField()
author = models.ForeignKey("User", ...)
# Validation ensures a comment has
# a thread or parent comment but not both
thread = models.ForeignKey("CommentThread", related_name="comments", ...)
parent = TreeForeignKey(
'self',
on_delete=models.CASCADE,
null=True,
blank=True,
related_name='children'
)
class MPTTMeta:
level_attr = 'mptt_level'
order_insertion_by=['name']
此模型使我可以向页面添加多个“根注释”,而且还可以将注释作为递归嵌套在每个注释下。
# Model Use Examples
thread = CommentThread()
page = Page(thread=thread)
# add page level root comments
comment1 = Comment(thread=thread, ...)
comment2 = Comment(thread=thread, ...)
comment3 = Comment(thread=thread, ...)
# add Comment Replies to comment #1
comment_reply1 = Comment(parent=comment1, ...)
comment_reply2 = Comment(parent=comment1, ...)
comment_reply3 = Comment(parent=comment1, ...)
当前方法-在python中
有效,但效率很低:
page = Page.objects.first()
total_comments = [c.get_descendant_count() for c in page.thread.comments.all()]
我不确定如何使用查询集和注释来实现。
我知道每个mptt模型也都有一个treed_id
,所以我想我需要构建一个更复杂的子查询。
要仅获取根注释的数量(不包括嵌套注释),我可以这样做:
pages = Page.objects.all().annotate(num_comments=models.Count("thread__comments"))
num_root_comments = pages[0].num_comments
再一次,目标是获取所有注释,包括嵌套的注释:
# Non working code - this kind of pseudo queryset code of what I am trying:
all_page_comments = Comment.objects.filter(tree_id__in= (Page.thread__comments__tree_id))
Page.objects.all().annotate(num_comments=Count(Subquery(all_page_comments))
在此先感谢您提供的帮助。
由于下面的@andrey回答,得到了一个可行的解决方案。 不确定它是否最佳,但似乎在单个查询中返回正确的值。
threads = CommentThread.objects.filter(
id=models.OuterRef("thread")
).annotate(
comment_count=models.Sum(
Floor((models.F("comments__rght") - models.F("comments__lft") - 1) / 2)
)
)
qs_pages_with_comment_count = (
Page.objects
.annotate(
comment_count=models.Subquery(
threads.values("comment_count")[:1], output_field=models.IntegerField()
)
)
# Count from subquery included count of descendents of
# each "root" comment but not the root comment itself
# so we add number of root comments per thread on top
.annotate(
comment_count=models.F("comment_count")
+ models.Count("thread__comments", distinct=True)
)
)
答案 0 :(得分:2)
queryset.annotate(
descendants_count=Floor((F('rght') - F('lft') - 1) / 2)
).values(
'descendants_count'
).aggregate(
total_count=Count('descendants_count')
)
让我解释一下
首先,get_descendant_count
的当前方法仅操作现有数据,因此我们可以在Queryset中使用它。
def get_descendant_count(self):
"""
Returns the number of descendants this model instance has.
"""
if self._mpttfield('right') is None:
# node not saved yet
return 0
else:
return (self._mpttfield('right') - self._mpttfield('left') - 1) // 2
这是当前mptt模型的方法。在queryset中,我们确保所有实例都已保存,因此我们将跳过该实例。
下一步是将数学运算转换为db表达式。
在Django 3.0中出现了Floor
expression。但是我们甚至可以在1.7中使用它(就像我一样)
from django.db.models.lookups import Transform
class Floor(Transform):
function = 'FLOOR'
lookup_name = 'floor'
如果需要,您可以重构它以使用self._mpttfield('right')
模拟而不是硬编码的rght, lft
并将其作为Manager
方法
测试。我的后代拥有最重要的元素
In [1]: m = MenuItem.objects.get(id=settings.TOP_MENU_ID)
In [2]: m.get_descendant_count()
Out[2]: 226
In [3]: n = m.get_descendants()
In [4]: n.annotate(descendants_count=Floor((F('rght') - F('lft') - 1) / 2)).values('descendants_count').aggregate(total_count=Count('descendants_count'))
Out[4]: {'total_count': 226}