0
我想修改我的數據庫的現有行,這是一個名爲「CharacterSkill」具有獨特的約束在一起的中介表:修改unique_together結合的形式Django的
models.py
class CharacterSkill(models.Model):
character = models.ForeignKey(Character, on_delete=models.CASCADE)
level = models.IntegerField(default=0)
skill = models.ForeignKey(Skill, on_delete=models.CASCADE)
class Meta:
unique_together = ("character","skill")
我做了一個表格來更改水平,但我無法保存表單,我有兩個領域「字符」 &「技能」兩個錯誤消息:
Select a valid choice. That choice is not one of the available choices.
燦你幫忙嗎? :)
我的形式:
class SkillCreateForm(forms.ModelForm):
class Meta:
model = CharacterSkill
fields = ('skill','level','character',)
我的觀點:
def skill_update(request,skillpk,instancepk):
form = SkillCreateForm(request.POST)
user = User.objects.get (id = request.user.id)
instance = Character.objects.get (id = instancepk)
skill = CharacterSkill.objects.get(id = skillpk)
data = {'character' : instance,
'skill' : skill.skill,
'level' : skill.level,
}
if form.is_valid():
form.save()
return redirect('persomaker:skill_list', instance.id)
else:
form = SkillCreateForm(data)
#form.fields['skill'].widget = HiddenInput()
#form.fields['character'].widget = HiddenInput()
return render(request, 'character/create_skill.html',
{'instance':instance,
'skill':skill,
'form': form,})