30
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).
- [Django]-Django – {% csrf_token %} was used in a template, but the context did not provide the value
4
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).
- [Django]-Django 2.0 – Not a valid view function or pattern name (Customizing Auth views)
- [Django]-405 "Method POST is not allowed" in Django REST framework
- [Django]-Django-reversion and related model
3
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.
- [Django]-Django – Rotating File Handler stuck when file is equal to maxBytes
- [Django]-Django Multiple Authentication Backend for one project
- [Django]-Celery. Decrease number of processes
1
If you have put required=True
in your forms field or in the model blank=False
so is_valid()
should return False.
- [Django]-Django: Implementing a Form within a generic DetailView
- [Django]-Django Admin app or roll my own?
- [Django]-Django-taggit – how do I display the tags related to each record
- [Django]-Handle `post_save` signal in celery
- [Django]-Django project models.py versus app models.py
- [Django]-Add rich text format functionality to django TextField
Source:stackexchange.com