#22921: Model.clean_fields incorrectly skips validation for fields where null
value
is not allowed
----------------------------------------------+----------------------------
Reporter: silveroff@… | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Database layer (models, ORM) | Version: master
Severity: Normal | Keywords: field
Triage Stage: Unreviewed | validation, model
Easy pickings: 0 | Has patch: 0
| UI/UX: 0
----------------------------------------------+----------------------------
I'm curious why does this method skips validation for a field with None
value. As a side effect if field has null=False, skipping validation will
result in DatabaseError saying that coulmn cannot be null. Why don't we
check if None is a valid value for a field, taking into account current
status of ``null`` option? This would save developers from nasty bugs and
boilerplate in their model forms.
{{{
def clean_fields(self, exclude=None):
"""
Cleans all fields and raises a ValidationError containing
message_dict
of all validation errors if any occur.
"""
if exclude is None:
exclude = []
errors = {}
for f in self._meta.fields:
if f.name in exclude:
continue
# Skip validation for empty fields with blank=True. The
developer
# is responsible for making sure they have a valid value.
raw_value = getattr(self, f.attname)
if f.blank and raw_value in f.empty_values:
continue
try:
setattr(self, f.attname, f.clean(raw_value, self))
except ValidationError as e:
errors[f.name] = e.error_list
if errors:
raise ValidationError(errors)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/22921>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/062.33725f3fdf5038972f850e993a9ec91b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.