Closed BigglesZX closed 4 years ago
@BigglesZX sorry for taking a while to get back to you. It looks like Django does not permit empty_permitted
to be passed in through form_kwargs
. In Django 2.2 empty forms are constructed as below:
django/forms/formsets.py
class BaseFormSet:
...
@property
def empty_form(self):
form = self.form(
auto_id=self.auto_id,
prefix=self.add_prefix('__prefix__'),
empty_permitted=True,
use_required_attribute=False,
**self.get_form_kwargs(None)
)
self.add_fields(form, None)
return form
I don't think this library should document what fields Django will or won't allow as part of form_kwargs
as this may change between Django versions. Nevertheless thanks for reporting this. The below change in Django would address this, but would need to be made in Django:
class BaseFormSet:
...
@property
def empty_form(self):
form_kwargs = self.get_form_kwargs(None).copy()
form_kwargs.update({
'auto_id': self.auto_id,
'prefix': self.add_prefix('__prefix__'),
'empty_permitted': True,
'use_required_attribute': False,
})
form = self.form(**form_kwargs)
self.add_fields(form, None)
return form
You could still use form_kwargs
if you also override the empty_form
method of your model formset class as above.
Just ran into a persnickety issue when using multiple
initial
data to prepopulate my formset. Only the first form in the formset was being saved – the rest had emptycleaned_data
.It turns out that Django's detection of new/changed data was responsible – because the values in the formset's forms had not changed from their
initial
, Django didn't include them in the form data and so didn't try to save them.I eventually found this SO answer which pointed to the solution of setting
empty_permitted = False
on the forms. However, I was not able to useformset_kwargs
to set this property (see below).Simplified code for illustration:
I was not able to use
formset_kwargs
with theform_kwargs
key on myInlineFormSetFactory
subclass to setempty_permitted
as is described in the documentation on formset customization. When I tried that I got a TypeError:ModelFormMetaclass object got multiple values for keyword argument 'empty_permitted'
. Hence the use of__init__
above. Not sure if I was doing something wrong, but the alternative works nonetheless.Perhaps the documentation could be updated to make reference to this form property? Hope this is helpful to someone encountering the same problem. Thanks for a very useful library.