Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How does validation work in django rest framework?


Asked by Chana Howell on Dec 02, 2021 Django



This can be achieved by using validator functions and validator classes. Validation in Django REST framework serializers is handled a little differently to how validation works in Django's ModelForm class. With ModelForm the validation is performed partially on the form, and partially on the model instance.
Subsequently,
Validators are used to validate the data whether it is semantically valid or not. Django REST supports both serializers and model serializers. Serializers provides basic validation for fields, In some cases we need to write custom validations for fields .
Thereof, REST framework includes a few helper classes that extend Django's existing test framework, and improve support for making API requests. Extends Django's existing RequestFactory class. The APIRequestFactory class supports an almost identical API to Django's standard RequestFactory class.
In fact,
To create a basic serializer one needs to import serializers class from rest_framework and define fields for a serializer just like creating a form or model in Django. This way one can declare serializer for any particular entity or object based on fields required.
Keeping this in consideration,
With REST framework the validation is performed entirely on the serializer class. This is advantageous for the following reasons: It introduces a proper separation of concerns, making your code behavior more obvious.