gpt4 book ai didi

python - Django select_related 查询

转载 作者:行者123 更新时间:2023-11-28 23:25:15 28 4
gpt4 key购买 nike

我有以下两个模型:

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)
fixturematchday = models.ForeignKey('straightred.StraightredFixtureMatchday', db_column='fixturematchday')
hometeamscore = models.IntegerField(null=True)
awayteamscore = models.IntegerField(null=True)
soccerseason = models.ForeignKey('straightred.StraightredSeason', db_column='soccerseasonid', related_name='fixture_season')


def __unicode__(self):
return self.fixtureid

class Meta:
managed = True
db_table = 'straightred_fixture'

class UserSelection(models.Model):
userselectionid = models.AutoField(primary_key=True)
campaignno = models.CharField(max_length=36,unique=False)
user = models.ForeignKey(User, related_name='selectionUser')
teamselection1or2 = models.PositiveSmallIntegerField()
teamselectionid = models.ForeignKey('straightred.StraightredTeam', db_column='teamselectionid', related_name='teamID')
fixtureid = models.ForeignKey('straightred.StraightredFixture', db_column='fixtureid')


class Meta:
managed = True
db_table = 'straightred_userselection'

我正在使用以下查询:

    prevWeekTeams = UserSelection.objects.select_related().filter(soccerseason=1025,fixturematchday=5,user=currentUserID).order_by('teamselection1or2')

运行时出现以下错误:

Cannot resolve keyword 'soccerseason' into field. Choices are: campaignno, fixtureid, fixtureid_id, teamselection1or2, teamselectionid, teamselectionid_id, user, user_id, userselectionid

我知道这行不通,但我不太明白如何在赛程表中引用 soccerseason。任何帮助,将不胜感激。非常感谢,艾伦。

附言

如果您需要链接到的其他两个模型,请告诉我。

最佳答案

您只需要过滤具有该格式的外国模型的字段

field-that-reference-to-foreign-model__foreign-model-field 在你的情况下 fixtureid__soccerseasonThe docs它充其量描述的地方。

你应该做的是

prevWeekTeams = UserSelection.objects.filter(fixtureid__soccerseason=1025,fixtureid__fixturematchday=5,user=currentUserID).order_by('teamselection1or2')

关于python - Django select_related 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39647699/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com