I want to define my own validation routine for a particular field of a Django model. I want the error message to be displayed in the admin form but I also want the same validation to take place if the entity is saved by own python code. Is there a way to do this without breaking the DRY principle?
相关问题
- Django __str__ returned non-string (type NoneType)
- Django & Amazon SES SMTP. Cannot send email
- Django check user group permissions
- Django restrict pages to certain users
- UnicodeEncodeError with attach_file on EmailMessag
相关文章
- TypeError: 'BaseQuery' object is not calla
- Profiling Django with PyCharm
- Why doesn't Django enforce my unique_together
- MultiValueDictKeyError in Django admin
- Django/Heroku: FATAL: too many connections for rol
- Django is sooo slow? errno 32 broken pipe? dcramer
- Django: Replacement for the default ManyToMany Wid
- Upgrading transaction.commit_manually() to Django
If you want to validate an individual field, you can write a validator and add it to your model field.
The validator will be run for the field whenever the model's
full_clean
method is called. It will be run whenever a model form is validated (including in the Django admin), but it will not automatically run when the model instance is saved - you must callfull_clean
manually in python code.If you wanted to force the validator to run whenever the model is saved, then you could override the save method and call
full_clean
there. Note that this would cause the validation to run twice when using model forms and the django admin.