Formsets have been driving me crazy for weeks because the documentation isn’t really helpful using them i keep getting a validation error every time i want to save a formset and i think i have everything in place.
Here is the traceback
Traceback (most recent call last):
File "/home/jojoe/.local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/jojoe/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/jojoe/.local/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jojoe/.local/lib/python3.8/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/jojoe/portfolio_app/portfolioapp/portfolio/views.py", line 63, in portfolio_form
if skillset.is_valid():
File "/home/jojoe/.local/lib/python3.8/site-packages/django/forms/formsets.py", line 308, in is_valid
self.errors
File "/home/jojoe/.local/lib/python3.8/site-packages/django/forms/formsets.py", line 288, in errors
self.full_clean()
File "/home/jojoe/.local/lib/python3.8/site-packages/django/forms/formsets.py", line 329, in full_clean
for i in range(0, self.total_form_count()):
File "/home/jojoe/.local/lib/python3.8/site-packages/django/forms/formsets.py", line 112, in total_form_count
return min(self.management_form.cleaned_data[TOTAL_FORM_COUNT], self.absolute_max)
File "/home/jojoe/.local/lib/python3.8/site-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/jojoe/.local/lib/python3.8/site-packages/django/forms/formsets.py", line 92, in management_form
raise ValidationError(
django.core.exceptions.ValidationError: ['ManagementForm data is missing or has been tampered with']
views
if request.method == 'POST':
print(request.POST)
person_form = PersonForm(request.POST, request.FILES)
occupation_form = OccupationForm(request.POST)
contact_form = ContactForm(request.POST)
skillset = SkillsFormSet(request.POST, prefix='skills')
workexpset = WorkExpFormSet(request.POST, prefix='work-exp')
acadexpset = AcadExpFormSet(request.POST, prefix='acad-exp')
# if person_form.is_valid() and occupation_form.is_valid() and skillset.is_valid() and workexpset.is_valid() and acadexpset.is_valid() and contact_form.is_valid():
if person_form.is_valid():
person=person_form.save(commit=False)
person.user=request.user
person.save()
if occupation_form.is_valid():
occupation=occupation_form.save(commit=False)
occupation.user=request.user
occupation.save()
if contact_form.is_valid():
contact=contact_form.save(commit=False)
contact.user=request.user
contact.save()
if skillset.is_valid():
instances = skillset.save(commit=false)
for instance in instances:
instance.user=request.user
instance.save()
if workexpset.is_valid():
instances = workexpset.save(commit=false)
for instance in instances:
instance.user=request.user
instance.save()
if acadexpset.is_valid():
instances = acadexpset.save(commit=false)
for instance in instances:
instance.user=request.user
instance.save()
template
<div>
{{skillset.management_form }}
{{skillset.non_form_errors}}
<div id="skill_set">
{% for skill in skillset %}
{{skill|crispy}}
{% endfor %}
</div>
<small style="display:none" id="alert-skill" class="alert alert-success">A skill has been added!</small>
<div id="empty_skill_form" style="display:none">
{{ skillset.empty_form }}
</div>
<button id="addskill">add</button>
</div>
the other templates are the same so i don’t feel there is need for that