Context: I’m building a basic bookkeeping application, just updated to Django 5.0.1.
I have a complicated custom CreateView
which render a form for an Entry
as well as the rows in that entry, EntryRow
s. The post
method of this view has the following code snippet:
entryform = EntryForm(request.POST)
entryrow_formset = EntryRowFormSet(request.POST)
if entryform.is_valid() and entryrow_formset.is_valid():
entry = entryform.save()
entryrow_instances = entryrow_formset.save(commit = False)
This works perfectly!
I built a test for this view. I used the view to trigger this post
method, logged the request.POST
and copied the whole thing to be hardcoded in my test. However, the following snippet
data = {
'journal': ['journ1'], 'notes': [''], 'form-TOTAL_FORMS': ['2'],
'form-INITIAL_FORMS': ['0'], 'form-MIN_NUM_FORMS': ['0'], 'form-MAX_NUM_FORMS': ['1000'],
'form-0-id': [''], 'form-0-date': ['2023-12-27'], 'initial-form-0-date': ['2023-12-27', '', ''],
'form-0-ledger': ['test1'], 'form-0-account': ['acc1'], 'form-0-value': ['2'], 'form-1-id': [''],
'form-1-DELETE': [''], 'form-1-date': ['2023-12-27'], 'form-1-ledger': ['test2'],
'form-1-account': ['acc2'], 'form-1-value': ['-3']
}
formset = EntryRowFormSet(data)
print(formset.is_valid())
print(formset.non_form_errors())
Bafflingly, this formset always yields the error: ManagementForm data is missing or has been tampered with. Missing fields: form-TOTAL_FORMS, form-INITIAL_FORMS, form-MIN_NUM_FORMS, form-MAX_NUM_FORMS. You may need to file a bug report if the issue persists.
Those fields are clearly in the data
right? Is this a bug in Django?
For reference, here is the code for the EntryRowForm
and the EntryRowBaseFormSet
:
The EntryRowFormSet
is constructed using the modelformset_factory
with default settings.
Thanks in advance for any help!