我有以下型号:计数一定的选择出现在车型数量
class Team(models.Model):
# stuff
class Event(models.Model):
# stuff
class Award(models.Model):
award_type_choices = (
(1, "some string"), # a lot of these
(2, "some other str"),
)
award_type = models.CharField(choices=award_type_choices)
specific_choices = (
(1, "some string"), # etc
# only a few of these, but all of these can be found inside award_type_choices.
)
event = models.ForeignKey(Event)
recipients = models.ManyToManyField(Team)
我想计数/注释的次Team
数量已经赢得了specific_choices
序列的下面,一个奖项。我可以过滤已通过这段代码获得奖项的球队:
reversed_choices = dict((v, k) for k, v in Award.specific_choices)
Team.objects.filter(award__award_type__in=reversed_choices.values())
但是,我不知道我应该怎么计算接近这些。我之前已经使用了Count
,F
和ExpressionWrapper
,但并没有广泛地知道如何做到这一点。
我想我可以通过抛出相同的参数filter
成Count
对象接近它,但只要我打它,我意识到这是行不通的,即:
Team.objects.annotate(num_specifics=Count('award__award_type__in=Award.specific_choices'))
任何帮助将不胜感激。
对不起,我不认为我是很清晰。我正在寻找每个团队的“注释”字段,这个字段的次数是他们有多少次是'specific_choices'字段的'Award'。此外,'specific_choices'只是一个在其中包含'int,str'元组的序列。无法调用'all()'。 – Justin
然后它只是Team.objects.annotate(num_specifics = c),其中c是上述返回的值。 – dmitryro
这不起作用; AFAIK,你不能在注释里面加入查询。此外,所做的是让许多球队赢得这些特定奖项,而不是赢得多少奖金。我正在寻找后者。 – Justin