是否可以用复杂的查询来注释 Django ORM 查询
我有三个模型,竞争者、周和投票,每个竞争者都可以根据周进行投票,
class Contender(models.Model)
week = models.ManyToManyField(Week)
class Week(models.Model):
date_start = models.DateField()
class Vote(models.Model):
contender = models.ForeignKey(Contender)
week = models.ForeignKey(Week)
我想向竞争者添加一些东西,所以我这样做了:
c = Count('vote', vote__week__date_start = "2011-01-03")
contenders = Contender.objects.all().annotate(vote_count=c).order_by('-vote_count')
contenders[0].vote_count
问题是当我添加另一周的投票时(有不同的 date_start) .vote_count 值发生变化,因此我传递给 Count 对象的额外参数似乎并不重要。
如何在 Django ORM 中进行这种类型的注释?
I have three models, Contender, Week and Vote, each contender can have votes based on week,
class Contender(models.Model)
week = models.ManyToManyField(Week)
class Week(models.Model):
date_start = models.DateField()
class Vote(models.Model):
contender = models.ForeignKey(Contender)
week = models.ForeignKey(Week)
I would like to add something to the Contender, so I did this:
c = Count('vote', vote__week__date_start = "2011-01-03")
contenders = Contender.objects.all().annotate(vote_count=c).order_by('-vote_count')
contenders[0].vote_count
the problem is that when I add a vote with another Week (that has diferent date_start) the .vote_count value is changes and thus it seems like the extra parameters I pass to the Count object does not matter.
How do I do this type of annotation in the Django ORM?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以从投票开始:
另外,你可以做一些非规范化 -
在其中添加和计算投票(覆盖 Vote.save() 或使用 post_save 信号),然后你就会这样做:
如果你这样做,性能会更有效经常统计。
You could start from Vote:
Also, you can do some denormalization - add
and count votes in it (overriding Vote.save() or using post_save signal), then you will just do:
It will be much more efficient performancewise if you do such statistics often.