If you are using model forms, the full_clean
will be called as part of the form validation step.
In general, there isn't a convenience method that calls full_clean
, but you could write your own.
Question
So I've got the following code:
class MyModel(models.Model):
# Text language.
ENGLISH = 'eng'
FRENCH = 'fr'
LANGUAGES_CHOICES = [
(ENGLISH, 'English'),
(FRENCH, 'French'),
]
language = models.CharField(
max_length=max(len(language) for language in LANGUAGES_CHOICES),
choices=LANGUAGES_CHOICES,
blank=False,
null=True)
When I do MyModel(language='invalid').save()
or MyModel.objects.create(language='invalid')
, the model is saved without complaint. Is there any convenience method that is equivalent to Model.objects.create
that performs a full_clean
before it saves?
Solution
If you are using model forms, the full_clean
will be called as part of the form validation step.
In general, there isn't a convenience method that calls full_clean
, but you could write your own.
OTHER TIPS
Model.clean(): This method should be used to provide custom model validation, and to modify attributes on your model if desired. For instance, you could use it to automatically provide a value for a field, or to do validation that requires access to more than a single field: