我有一个脚本可以将足球装置插入我的mySQL数据库。如果我不试图拉入赛季,那就行了。但是,当我把季节性打入其中时,我不确定为什么。任何帮助将不胜感激。
模型如下:
class StraightredSeason(models.Model):
seasonid = models.IntegerField(primary_key = True)
seasonyear = models.CharField(max_length = 4)
seasonname = models.CharField(max_length = 36)
def __unicode__(self):
return self.seasonid
class Meta:
managed = True
db_table = 'straightred_season'
class StraightredTeam(models.Model):
teamid = models.IntegerField(primary_key=True)
teamname = models.CharField(max_length=36)
country = models.CharField(max_length=36,null=True)
stadium = models.CharField(max_length=36,null=True)
homepageurl = models.TextField(null=True)
wikilink = models.TextField(null=True)
teamcode = models.CharField(max_length=5,null=True)
teamshortname = models.CharField(max_length=24,null=True)
currentteam = models.PositiveSmallIntegerField(null=True)
def __unicode__(self):
return self.teamname
class Meta:
managed = True
db_table = 'straightred_team'
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.IntegerField(null=True)
spectators = models.IntegerField(null=True)
hometeamscore = models.IntegerField(null=True)
awayteamscore = models.IntegerField(null=True)
homegoaldetails = models.TextField(null=True)
awaygoaldetails = models.TextField(null=True)
hometeamyellowcarddetails = models.TextField(null=True)
awayteamyellowcarddetails = models.TextField(null=True)
hometeamredcarddetails = models.TextField(null=True)
awayteamredcarddetails = models.TextField(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'
StraightredSeason表包含以下内容:
mysql> select * from straightred_season;
+----------+------------+------------+
| seasonid | seasonyear | seasonname |
+----------+------------+------------+
| 1025 | 16/1 | EPL |
+----------+------------+------------+
但每当我运行下面的代码时,我都会收到以下错误:
无法指定“1025”:“StraightredFixture.soccerseason”必须是“StraightredSeason”实例。
fixtureUpdate = StraightredFixture(fixtureid=fixture['Id'],
away_team_id = fixture['AwayTeam_Id'],
home_team_id = fixture['HomeTeam_Id'],
fixturedate = fixture['Date'],
fixturestatus = fixture['Time'],
fixturematchday = fixture['Round'],
spectators = fixture['Spectators'],
hometeamscore = fixture['HomeGoals'],
awayteamscore = fixture['AwayGoals'],
homegoaldetails = fixture['HomeGoalDetails'],
awaygoaldetails = fixture['AwayGoalDetails'],
hometeamyellowcarddetails = fixture['HomeTeamYellowCardDetails'],
awayteamyellowcarddetails = fixture['AwayTeamYellowCardDetails'],
hometeamredcarddetails = fixture['HomeTeamRedCardDetails'],
awayteamredcarddetails = fixture['AwayTeamRedCardDetails'],
soccerseason = 1025)
答案 0 :(得分:1)
您无法将.html
对象设置为ForeignKey
。您可以通过在字段中附加int
来设置ForeignKey
值(与FK对象关联的ID):
_id
答案 1 :(得分:0)
尝试将soccerseason = 1025
替换为soccerseason_id = 1025
。 soccerseason
是与StraightredSeason
的关系,因此您在尝试设置此属性时应提供该实例。