I am creating a django application that uses both the Django Rest Framework and simple django views as an entry point for users.
I want to check both the independent fields of my models and the objects as a whole. For example:
Field: The entered license fee is correct based on the regular expression function. No relation to other fields.
Object: entered zipcode is valid for this country. Refers to the zipcode and country in the model.
For the DRF-API, I use ModelSerializers, which automatically calls all validators that I placed in my model, for example:
class MyModel(models.Model):
licence_plate = CharField(max_length=20, validators=[LicencePlateValidator])
Since the validator is specified in the model, the POSTS API (because I use ModelSerializer), as well as objects created in the django admin backend, are checked.
But when I want to introduce object level checking, I need to do this in the serializer of the validate () method, which means that objects are checked only in the API.
I will also have to override the model persistence method to validate objects created on the Django admin page.
Question: It seems to me that this is a little messy, is there one point where I can put checks at the object level so that they run in the API and on the admin page, for example, I did with the check at the field level (I only need to put them in my model declaration and everything is processed)
source
share