Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

258
Views
modify unique_together bound form django

I'm trying to modify an existing row of my database, it's a from an intermediary table named "CharacterSkill" with an unique together constraint :

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")

I did a form to change the level but I'm unable to save the form, I have two errors messages from both fields "character" & "skill":

Select a valid choice. That choice is not one of the available choices.

Can you help on that please ? :)

My form :

class SkillCreateForm(forms.ModelForm):
    class Meta:
        model = CharacterSkill
        fields = ('skill','level','character',)

my view :

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,})
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

based on a book, I changed the way I bounded the form to use instance :

def skill_update(request,skillpk,instancepk):

    user = User.objects.get (id = request.user.id)
    instance = Character.objects.get (id = instancepk)
    skill = CharacterSkill.objects.get(id = skillpk)
    if request.method == "POST":
        form = SkillModifyForm(request.POST,instance = skill,)
        print (skill)
        if form.is_valid():
            form.save()
            return redirect('persomaker:skill_list', instance.id)
    else:
        form = SkillModifyForm(instance = skill,)
    return render(request, 'character/create_skill.html',
    {'instance':instance,
    'skill':skill,
    'form': form,})
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!