
If request. """ return serializer_class ( *args, **kwargs ) If not mind your own business and move on Return serializer_class ( *args, **kwargs ) """ But what I like to have is a custom structure of all my models together. When adding serializers and viewsets to DRF, I only get the fields associated with that Model. copy ( )ĭraft_request_data = nameĭraft_request_data = surname I'm searching for a solution to create a serializer / API Endpoint to represent data in a custom order. get ( "surname" ) ) : # Copy and manipulate the requestĭraft_request_data = self.

Intercept the request and see if it needs tweaking When subclassing CreateAPIView we get access to all the methods defined in CreateModelMixin and GenericAPIView:ĭef get_serializer (self, *args, **kwargs ) : # leave this intact NOTE: if you want to test this view check out DRF: testing POST requests. So where do we intercept and swap request.data? But there's no way to swap back request.data with your own object because at this stage request is immutable too.

That is a GET request on user model returns the attributes of user object in JSON format if I use JSON serializer. The only way to change it is to copy the object and modify the copy. using Django Rest framework to serialize custom data types and return response Ask Question 8 Most of the tutorials on Django Rest Framework explains using the Django models and doing CRUD operations. Request.data in fact is a Django QueryDict which turns out to be immutable. If only was that easy! If we run this view we get AttributeError: This QueryDict instance is immutable. create (request, *args, **kwargs ) return self. serializers import ContactSerializerĬlass ContactCreateAPI (CreateAPIView ) :ĭef post (self, request, *args, **kwargs ) : if (name : = request. generics import CreateAPIViewįrom library. Custom Validation for serializer fields Django REST supports both serializers and model serializers.
