过滤我的注释

时间:2012-02-13 20:31:28

标签: python django annotations django-views

我有一大堆代码可以让所有玩家受到伤害。

teams = Team.objects.annotate(team_count=Count('Team played for')).order_by('city')

但我遇到了一个问题。它目前正在增加所有伤病,但我想只计算目前受伤的球员,或者更具体地说...... returned=False

以下是我的模型以获得更多背景信息。

Team models.py

class Team(models.Model):
    city = models.CharField(verbose_name='City', max_length=30, null=True, blank=True)
    team_name =  models.CharField(verbose_name='Team Name', max_length=30, null=True, blank=True)
    team_logo = models.ImageField(upload_to='images/', null=True, blank=True)
    team_affiliation = models.ForeignKey('self', null=True, blank=True)

    def __unicode__(self):
        return u'%s %s' % (self.city, self.team_name)

EDIT添加了播放器models.py

class Player(models.Model):
    first_name = models.CharField(verbose_name='First Name', max_length=30, null=True, blank=True)
    last_name = models.CharField(verbose_name='Last Name', max_length=30, null=True, blank=True)
    team = models.ForeignKey(Team, related_name='Team played for')
    position = models.ForeignKey(Position, related_name="Position")
    pob = models.CharField(verbose_name='Place of Birth', max_length=100, null=True, blank=True)
    dob = models.DateField()
    age =  models.IntegerField(null=True, blank=True)
    height = models.CharField(verbose_name='Height', max_length=10, null=True, blank=True)
    weight = models.CharField(verbose_name='Weight', max_length=10, null=True, blank=True)
    drafted_by = models.ForeignKey(Team, related_name='drafted by', null=True, blank=True)

    def __unicode__(self):
        return u'%s %s' % (self.first_name, self.last_name)

Injury models.py

class PlayerInjury(models.Model):
    player =  models.ForeignKey(Player)
    injury_type = models.ForeignKey(Injury)
    injury_date = models.DateField(verbose_name='Injured On', null=True, blank=True)
    description = models.CharField(verbose_name='Description', max_length=180, null=True, blank=True)
    status = models.ForeignKey(Status)
    projected_return = models.DateField(verbose_name='Projected Return Date', null=True, blank=True)
    hide = models.BooleanField(default=False)
    returned = models.BooleanField(default=False)
    timestamp = models.DateTimeField(auto_now_add=True)
    source = models.URLField(max_length=200, null=True, blank=True)
    source_label = models.CharField(null=True, blank=True, max_length=200)

    def __unicode__(self):
        return u'%s - %s' % (self.player, self.injury_type)

有没有办法可以过滤注释,以便只计算当前受伤的玩家?

1 个答案:

答案 0 :(得分:2)

在调用注释之前只需过滤:.filter(player__playerinjury__returned=False).annotate(...)