Skip to content Skip to sidebar Skip to footer

Django Save Only First Form Of Formset

I've looked through every similar question (and tried them), but still couldn't find answer. I have two models: class Project(models.Model): author = models.ForeignKey(settings

Solution 1:

Here is how it was solved:

  1. Included errors properly.
  2. Saw that second to last form lack required field (hiddenInput)
  3. Made changes in view so it looks like:

    formset_f = modelformset_factory(ProjectPage, form=ProjectPageForm, extra=3)
    
    formset = formset_f(queryset=ProjectPage.objects.filter(page_project__id=proj), initial =[{'page_project': proj}, {'page_project': proj}, {'page_project': proj}])
    

Initial values now match number of extra forms - every form got it's own foreign key.

Probably there is a better solution, but the the problem is found and solved for me!

Solution 2:

My problem was that when I tried to render every form of the formset manually I added an unneded <form></form> html element

wrong:

{ form.management_form }}

 {% for form in formset %}
   <formclass="form-class">
       {{form.name}}
   </form>

right:

{ form.management_form }}

 {% for form in formset %}
   <divclass="form-class">
       {{form.name}}
   </div>

After that change my forms were recognized correctly.

Post a Comment for "Django Save Only First Form Of Formset"