过滤我的注释

发布于 2025-01-05 12:20:48 字数 2624 浏览 0 评论 0原文

我有这段代码,它将一支球队中所有受伤的球员加起来。

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)

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

I have this chunk of code which adds up all the players hurt on one team.

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

I am having an issue however. It is currently adding all injuries, but I would like to count just the players that are currently injured, or more specifically... where returned=False

Here are my models for a bit more context.

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 added player 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)

Is there a way I can filter the annotation so that it will only count the currently injured players?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

另类 2025-01-12 12:20:48

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

Just filter before calling annotate: .filter(player__playerinjury__returned=False).annotate(...).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文