也许我的搜索不对,但我还没有找到它......
我有三个型号。这是两个,另一个是用户模型:
class Comment(Model(20)):
text = TextField(max_length=160)
class Look(Model(20)):
comments = ManyToManyField(Comment, related_name='comments', editable=False)
likes = ManyToManyField(get_user_model(), related_name="look_likes", editable=False)
现在我想通过喜欢和评论的总数(计数(喜欢)+计数(评论))来命令我的外观。
我试过了:
most_wanted = Look.objects.all()
most_wanted = most_wanted.annotate(count_comments=Count('comments'))
most_wanted = most_wanted.annotate(count_likes=Count('likes'))
most_wanted = most_wanted.extra(select = { 'count': 'count_comments + count_likes' })
但注释字段不适用于额外的。我也尝试过:
most_wanted = Look.objects.all()
most_wanted = most_wanted.extra(select = { 'count': 'Count(comments) + Count(likes)' })
但相关字段(来自ManyToMany)也无法使用额外字段。
有没有?
答案 0 :(得分:0)
你无法在查询中真正做到这一点。但似乎没有必要:它是一个简单的添加,它可以在Python中完成。