2012-02-13 91 views
1

我有這塊代碼加起來就是所有在一個團隊中受傷的玩家。過濾我的註釋

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

但是我有問題。它目前正在增加所有傷病,但我想只計算目前受傷的球員,或者更具體地說......其中returned=False

這是我的模型,有點更多的上下文。

隊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) 

編輯添加球員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) 

損傷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) 

有沒有一種方法,我可以過濾註釋這樣它只會計算目前受傷的球員?

回答

2

在調用註釋之前進行過濾:.filter(player__playerinjury__returned=False).annotate(...)

+0

這不工作,因爲我使用團隊對象的註釋,它沒有作爲變量返回 – TheLifeOfSteve 2012-02-13 20:44:38

+0

看到更新 – 2012-02-13 20:51:30

+0

我仍然看到這個錯誤FieldError at/team /'它沒有像'player' – TheLifeOfSteve 2012-02-13 21:06:01