I'll start with the views . Views and views are classes in DRF where most of the application logic happens.
Eg. ModelViewSet is the class responsible for CRUD operations in response to the POST, PUT, PATCH, GET, DELETE HTTP methods.
Let's look at the default creation method from https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/mixins.py this method creates an instance of your model from the data (if valid) send via HTTP POST -method and save them in the database.
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) def perform_create(self, serializer): serializer.save()
This is what happens here.
self.get_serializer () creates a new instance of the serializer (you set the serializer class earlier), it takes request.data as an argument. request.data (this is important) dictionary . Dictionary is a general python data structure .
The serializer.is_valid () method validates request.data. If so, you can access serializer.data - also a dictionary .
The serializer.save () method creates and stores the actual instance of your model (Snippet) in the database. You can directly access an instance like this
instance = serializer.save()
Then you return the Response object populated with serializer.data back to the client.
As you can see, there are data in the views; no form data, JSON, XML, HTML, etc. You work with python data types, and the serializer is "translatable" for an instance of your specific model and vice versa. But the client sends the data (in your case) to the HTTP request as JSON.
JSONParser is responsible for converting JSON to a dictionary. Parsing is done inside the request class https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/request.py , note that this is not a standard django HttpRequest model.
You can install several parsers, then the request will select the correct one according to the header of the HTTP request: Content-type.
Secondly, you should return serializer.data back to the client as JSON, not a dictionary. What JSONRenderer does. It converts the dict to JSON and is implemented inside the Response class https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/response.py . You can also set several renderers, and then select the appropriate option according to the accepted header of the HTTP media type.
An example of a complete definition of a species might be:
from snippets.models import Snippet from snippets.serializers import SnippetSerializer from rest_framework.renderers import JSONRenderer from rest_framework.parsers import JSONParser class SnippetViewSet(viewsets.ModelViewSet): queryset = Snippet.objects.all() serializer_class = SnippetSerializer renderer_classes = (JSONRenderer, ) parser_classes = (JSONParser,)