繁体   English   中英

如何从QuerySet聚合对象

[英]How to aggregate objects from a QuerySet

我有一个Django模型用户,它具有单向自引用多对多关系:

following = models.ManyToManyField('self', through='Following', symmetrical=False, related_name='following_user')

我有另一个评论模型,它与用户有关系:

user = models.ForeignKey(User)

我想得到以下用户的所有评论。 如何从用户的QuerySet中获取所有注释? 像这样的东西?:

self.following.[somehow get all comments]

对于这样的查询,您需要将其转为:您需要注释,因此请从注释模型开始。

Comment.objects.filter(user__following=self)

这是最终为我工作的代码:

Comment.objects.filter(user__in=self.get_following())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM