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')
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/812883ba-9744-45de-b18f-ef527d3f1bc1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment