Django: How to check if the user left all fields blank (or to initial values)?
You should set required=False for all fields in the form and override the clean
method to do custom validation. See the current documentation for more.
Guess I have to answer my own question.
Apparently, there's an undocumented Form
function: has_changed()
>>> f = MyForm({})
>>> f.has_changed()
False
>>> f = MyForm({'name': 'test'})
>>> f.has_changed()
True
>>> f = MyForm({'name': 'test'}, initial={'name': 'test'})
>>> f.has_changed()
False
So this would do nicely as the replacement for form_is_blank()
(reverted of course).
If you have put required=True
in your forms field or in the model blank=False
so is_valid()
should return False.
To get this functionality work for subset of Forms used in the actual <form> tag you also need to define
class YourForm(forms.ModelForm):
def full_clean(self):
if not self.has_changed():
self._errors = ErrorDict()
return
return super(YourForm, self).full_clean()
so when the user is prompted to fix validation errors it doesnt display errors from the forms which you want to validate only if some value isn't blank (or default).