I’m creating a view to update information on a user using Model formsets and i get a validation error when i try to add new information. Here is a 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/porfolio_app/portfolioapp/portfolio/views.py", line 63, in portfolio_update_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']
This is the view logic.
def portfolio_update_form(request):
SkillsFormSet = modelformset_factory(Skills, form=SkillsForm)
if request.method == 'POST':
skillset = SkillsFormSet(request.POST, queryset=Skills.objects.filter(occupation_id=request.user.occupation.id))
if skillset.is_valid():
instances = skillset.save()
else:
skillset = SkillsFormSet()
context = {'skillset': skillset}
return render(request, 'portfolio/portfolio_update.html', context)