modlinltd/django-advanced-filters

ValueError while creating empty form

Closed this issue · 2 comments

Hi.
After upgrading to Django 2.1 and changing to the advance_filters development branch I got the following error.
...
File "/usr/local/lib/python3.6/site-packages/advanced_filters/forms.py", line 236, in forms
forms.append(self.empty_form) # add initial empty form
File "/usr/local/lib/python3.6/site-packages/advanced_filters/forms.py", line 218, in empty_form
empty_permitted=True,
File "/usr/local/lib/python3.6/site-packages/advanced_filters/forms.py", line 193, in init
super(AdvancedFilterQueryForm, self).init(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/forms/forms.py", line 104, in init
'The empty_permitted and use_required_attribute arguments may '
ValueError: The empty_permitted and use_required_attribute arguments may not both be True.
...
After debugging I found out that in the empty_form method inside advanced_filters/forms.py in the form constructor is probably missing the the use_required_attribute=False parameter.
After I added it, the advance_filters restarted to work.
This issue happens because in the django BaseForm init the use_required_attribute argument is set to true by default.

Could somebody add use_required_attribute=False to the form constructor in the empty_form method?

Thanks

I went through the defaults for formsets.BaseFormSet.empty_form property for a number of Django versions, and they all look very similar (In fact, it was last changed circa 6 years ago (for Django v1.6), also providing kwargs via the get_form_kwargs method, and the defaults have always more or less like below:

https://github.com/django/django/blob/master/django/forms/formsets.py#L187-L196

This means that we can fix this issue by simply removing our empty_form method and providing kwargs for the form (namely the given model_fields).

By the way, we ❤️ pull-requests !

Wouldn't it be faster to just adding the missing use_required_attribute=False argument? It works and no other changes are needed.