After submission my modelformset form is no longer blank and has added forms

21 views
Skip to first unread message

AJ Abrahamsen

unread,
Dec 24, 2015, 8:57:21 AM12/24/15
to Django users

When the database is empty the form shows up how it was intended to with fields for two parents and two children.  When I fill out the form and submit it I get no errors and I am redirected to the 'thanks' page.  If I check the database it shows that all forms were saved correctly.  The problem occurs at this point when I return to the form page.  There are now fields for four parents and four children.  There are blank fields for two of the parents and two of the children.  The other two parent and child forms are prepopulated with data that is already in the database.  If you fill in the blank fields and submit the form it will add the new information.  If you change anything in the prepopulated fields it will changed the existing information in the database.  If I add two more parents and two more children to the database and return to the form there will now be six parent and child forms (two blank and four prepopulated  with existing data).  I have tried manually entering the data in the admin portal and then going to the form but the same thing still happens.  It is as if the formset is pulling data out of the database.  How do I stop this?


I have attached two images that show what the form looks like when I return to it after submitting it.


  models.py  
from django.db import models


class Child(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    birthday = models.DateField()
    allergies = models.CharField(max_length=30)

    def __str__(self):
        return self.first_name + ' ' + self.last_name

class Parent(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    child = models.ManyToManyField(Child)

    def __str__(self):
        return self.first_name + ' ' + self.last_name

class Household(models.Model):
    household_name = models.CharField(max_length=30)
    parent = models.ManyToManyField(Parent)

    def __str__(self):
        return self.household_name

forms.py

    from django import forms
from .models import Child, Household, Parent

class HouseholdForm(forms.ModelForm):
    class Meta:
        model = Household
        fields = ('household_name',)

class ParentForm(forms.ModelForm):
    class Meta:
        model = Parent
        fields = ('first_name', 'last_name',)

class ChildForm(forms.ModelForm):
    class Meta:
        model = Child
        fields = ('first_name', 'last_name', 'birthday', 'allergies',)

views.py

    from django.forms import modelformset_factory
from django.shortcuts import get_object_or_404, render, redirect
from .forms import ChildForm, HouseholdForm, ParentForm 
from .models import Child, Parent, Household


def register(request):
    ParentFormSet = modelformset_factory(Parent, form=ParentForm, extra=2)
    ChildFormSet = modelformset_factory(Child, form=ChildForm, extra=2)
    if request.method == "POST":
        formset1 = ParentFormSet(request.POST, prefix="parent",)
        formset2 = ChildFormSet(request.POST, prefix="child",)
        if formset1.is_valid() and formset2.is_valid():
            formset1.save()
            formset2.save()
        return redirect('thanks',)
    else:
        formset1 = ParentFormSet(prefix="parent",)
        formset2 = ChildFormSet(prefix="child",)
    return render(request, 'register.html', {'formset1': formset1, 'formset2': formset2,})

def thanks(request):
    return render(request, 'thanks.html')
form_after_submission_part1.png
form_after_submission_part2.png
Reply all
Reply to author
Forward
0 new messages