Skip to content

Instantly share code, notes, and snippets.

@subsetpark
Created May 14, 2015 21:06
Show Gist options
  • Save subsetpark/70ce20f1503a829bba0a to your computer and use it in GitHub Desktop.
Save subsetpark/70ce20f1503a829bba0a to your computer and use it in GitHub Desktop.
django/db/models/base.py:102
def full_clean(self, exclude=None, validate_unique=True):
"""
Calls clean_fields, clean, and validate_unique, on the model,
and raises a ``ValidationError`` for any errors that occurred.
"""
errors = {}
if exclude is None:
exclude = []
else:
exclude = list(exclude)
try:
self.clean_fields(exclude=exclude)
except ValidationError as e:
errors = e.update_error_dict(errors)
# Form.clean() is run even if other validation fails, so do the
# same with Model.clean() for consistency.
try:
self.clean()
except ValidationError as e:
errors = e.update_error_dict(errors)
# Run unique checks, but only for fields that passed validation.
if validate_unique:
for name in errors.keys():
if name != NON_FIELD_ERRORS and name not in exclude:
exclude.append(name)
try:
self.validate_unique(exclude=exclude)
except ValidationError as e:
errors = e.update_error_dict(errors)
if errors:
raise ValidationError(errors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment