Django:Python访问onetone字段

2024-03-29 14:01:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

fixtures = StraightredFixture.objects.select_related().filter(soccerseason=soccerseason,fixturematchday=fixturematchday).order_by('fixturedate')
temp2 = fixtures[0].hometeamscore

它的作品,但它是使用“家庭队得分”从模型“StraightredFixture”,但我需要它从“StraightredFixtureLive”。有人能帮我修改代码吗,非常感谢,艾伦。你知道吗

两种模式如下:

class StraightredFixture(models.Model):
    fixtureid = models.IntegerField(primary_key=True)
    home_team = models.ForeignKey('straightred.StraightredTeam', db_column='hometeamid', related_name='home_fixtures')
    away_team = models.ForeignKey('straightred.StraightredTeam', db_column='awayteamid', related_name='away_fixtures')
    fixturedate = models.DateTimeField(null=True)
    fixturestatus = models.CharField(max_length=24,null=True)
    fixturematchday = models.ForeignKey('straightred.StraightredFixtureMatchday', db_column='fixturematchday')
    spectators = models.IntegerField(null=True)
    hometeamscore = models.IntegerField(null=True)
    awayteamscore = models.IntegerField(null=True)   

    def __unicode__(self):
        return self.fixtureid

    class Meta:
        managed = True
        db_table = 'straightred_fixture'

class StraightredFixtureLive(models.Model):
    fixtureid = models.OneToOneField(StraightredFixture, on_delete=models.CASCADE, primary_key=True,)
    hometeamscore = models.IntegerField(null=True)
    awayteamscore = models.IntegerField(null=True)

    def __unicode__(self):
        return self.fixtureid

    class Meta:
        managed = True
        db_table = 'straightred_fixturelive'

Tags: selftruedbmodelsnullfixturesclassrelated