下面的视图工作,这是利用“UpDeaTyOrxCube”功能,当存在的东西,因此更新记录辉煌。但是,如果不存在,并且必须使用创建选项来创建新记录,则会得到以下错误:
TypeError at /selectteams/1025/3/
'soccerseasonid_id' is an invalid keyword argument for this function
视图如下:
if request.method == 'POST':
form = SelectTwoTeams(request.POST,user=request.user)
if form.is_valid():
teamSelection1, created = UserSelection.objects.update_or_create(user_id=currentUserID,
fixturematchday=fixturematchday,
soccerseason_id=soccerseason,
teamselection1or2=1,
defaults={"campaignno":11,
"teamselection1or2":1,
"teamselectionid_id":request.POST['team1'],
"user_id":currentUserID,
"fixturematchday":fixturematchday,
"soccerseasonid_id":soccerseason})
teamSelection2, created = UserSelection.objects.update_or_create(user_id=currentUserID,
fixturematchday=fixturematchday,
soccerseason_id=soccerseason,
teamselection1or2=2,
defaults={"campaignno":11,
"teamselection1or2":2,
"teamselectionid_id":request.POST['team2'],
"user_id":currentUserID,
"fixturematchday":fixturematchday,
"soccerseasonid_id":soccerseason})
以下型号:
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 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')
fixturematchday = models.IntegerField(null=True)
soccerseason = models.ForeignKey('straightred.StraightredSeason', db_column='soccerseasonid', related_name='fixture_seasonUserSelection')
class Meta:
managed = True
db_table = 'straightred_userselection'
谢谢你的帮助,艾伦。
最佳答案
看起来它使用的是默认值,其中指定了“soccerseashid”而不是“soccerseashid”“应该行得通。