0

我不確定它是否可行,但我想要計算與具有精確vote_type屬性的模型相關的所有投票。如何在Django中用精確的字段值計算所有ForeignKey模型?

這裏的模型:

class Link(models.Model): 
    title  = models.CharField(max_length=200) 
    . . . 

class Vote(models.Model): 
    UP, DOWN = range(2) 
    TYPE_CHOICES = [(UP, "Upvote"), (DOWN, "DownVote")] 

    link = models.ForeignKey(Link, related_name='votes') 
    vote_type = models.IntegerField(choices=TYPE_CHOICES, db_index=True) 
    . . . 

我用這個統計所有選票:

Link.objects.annotate(ups=Count('votes')).order_by('-ups') 

,並想也許我可以用它來實現我想要的:

Link.objects.annotate(ups=Count('votes__vote_type__exact=1')).order_by('-ups') 

但似乎我不能在這裏使用filter()語法。

我正在使用Django 1.8.4。

回答

相關問題