djangodjango-formsformset

(Hidden field id) Select a valid choice. That choice is not one of the available choices. (Django)


I'm receiving this error when I try to submit two formsets. After I fill the form and click the save button, it gives the error:

(Hidden field id) Select a valid choice. That choice is not one of the available choices.

I'm trying to create dynamic form so that the user can add new sections and also new lectures inside the section when they click "Add" button. The adding new form function works well, I just have problem saving it to the database.

Views.py

def addMaterials(request, pk):

course = Course.objects.get(id=pk)
sections = CourseSection.objects.filter(course_id=pk)
materials = CourseMaterial.objects.filter(section__in=sections)

SectionFormSet  = modelformset_factory(CourseSection, form=SectionForm, extra=0)
sectionformset = SectionFormSet(request.POST or None, queryset=sections)

MaterialFormSet  = modelformset_factory(CourseMaterial, form=MaterialForm, extra=0)
materialformset = MaterialFormSet(request.POST or None, queryset=materials)

context = { 
  'course': course, 
  'sectionformset': sectionformset,
  'materialformset': materialformset,
  }

if request.method == "POST":
  if all([sectionformset.is_valid() and materialformset.is_valid()]):
    for sectionform in sectionformset:
      section = sectionform.save(commit=False)
      section.course_id = course.id
      section.save()
      for materialform in materialformset:
        material = materialform.save(commit=False)
        print(material)
        material.section_id = section #section.id or section.pk also doesn't work
        material.save()
    return('success')

return render(request, 'courses/add_materials.html', context)

Forms.py

class SectionForm(forms.ModelForm):
  class Meta:
    model = CourseSection
    fields = ['section_name', ]
    exclude = ('course_id', )

class MaterialForm(forms.ModelForm):
  class Meta:
    model = CourseMaterial
    fields = ['lecture_name', 'contents']

The second formset which is materialformset need the section id from the first formset hence why there is two loop in views. Can someone help me to solve this. I'm not sure how to fix it.

This is the what I'm trying to do. enter image description here


Solution

  • I'm new to django but I had to face with the same problem. My solution was to handle singularly each formset inside 'views.py'.

    In the template.html, create a tag for each formset you have, than inside that tag put <input type="submit" name="form1">(Note that name is important and must be different with the respect of the form you are submitting).

    Then in views.py, instead for writing if all([sectionformset.is_valid() and materialformset.is_valid()]), try like this:

    if 'form1' in request.POST:
         if sectionformset.is_valid():
             sectionformset.save()
             # other rows of your code
         return('success')
    
    if 'form2' in request.POST:
         if materialformset.is_valid():
             materialformset.save()
             # etc. etc.