Django Admin Interface Ignores Django Rest Serializer Validators

135 views
Skip to first unread message

C James Cook (Jim)

unread,
Jun 23, 2024, 11:32:00 PM6/23/24
to Django REST framework
If a serializer implemented based on the Django Rest framework ModelSerializer class defines a validator, will it be called when an API endpoint is accessed through the standard Django admin interface?

I have implemented validate function(s) in my backend implemented using the Django Rest framework. The validate function is called without a problem if I use a curl command or other React-based frontend to access the backend endpoints. However, if I use a browser to access the backend via the /admin endpoint, the validate functions are not called.

Near as I can figure, the Admin interface will call single field validation functions in the model from db.models.fields.__init__.run_validators() 
which is called by db.models.fields.__init__.clean 
which is called by db.models.base.clean_fields
which is called by db.models.base.full_clean
which is called by forms.models._post_clean
which is called by forms.forms.full_clean
and so on.


Viktor Yaremenko

unread,
Oct 12, 2024, 10:42:30 AM10/12/24
to Django REST framework
Hello
To summarize, the validation defined in DRF serializers won't be called during form validation in the Django Admin interface because the Admin uses its own validation methods based on forms and models. If you want consistent validation across the board, you should either place the validation in the model’s clean method or use a custom form that leverages your DRF serializers' validation logic. This way, you ensure that the same rules apply whether the model instance is being created or updated via the DRF API or the Django Admin interface.
Reply all
Reply to author
Forward
0 new messages