Django-admin clean_fields override, keeps previous data on submit
My question is quiet simple, I have a class in my app model that inherits from models.Model
.
I'm overriding the clean_fields
method of the django-admin, in order to execute some custom validation to my form.
The problem is that when it raises a ValidationError
from my custom validation, if the user tries to submit the form again with the correct information, it always keeps the data from the previous submit.
class SignedOffModelValidation(models.Model):
class Meta:
abstract = True
def clean_fields(self, exclude = None):
super(SignedOffModelValidation, self).clean_fields(exclude)
errors = {}
if getattr(self, self._meta.immutable_sign_off_field, False):
relation_fields = [
f for f in self._meta.fields
if isinstance(f,(models.ForeignKey,models.ManyToManyField,))
and not f.name.endswith('_ptr')
]
for field in relation_fields:
try:
field_value =开发者_JAVA技巧 getattr(self, field.name)
signed_off = getattr(
field_value,
field_value._meta.immutable_sign_off_field
)
except (AttributeError, ObjectDoesNotExist,):
continue
else:
if not signed_off:
msg = u'In order to signeoff, %s needs to be Signed Off' % \
(str(field_value),)
errors[field.name] = ([msg])
if errors:
raise ValidationError(errors)
Any help would be appreciated!
Best Regards
You should use the clean()
method rather than clean_fields()
. This is pretty clear in the Django documentation.
精彩评论