Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the self.get_serializer method come from in the Django REST Framework?

Tags:

In the DRF source code, there's a get_serializer method. It wasn't inherited from object and it's not a method in the CreateModelMixin class. Where does this method come from?

serializer = self.get_serializer(data=request.data) 

Here's the larger chunk of code for context.

from __future__ import unicode_literals  from rest_framework import status from rest_framework.response import Response from rest_framework.settings import api_settings   class CreateModelMixin(object):     """     Create a model instance.     """     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()      def get_success_headers(self, data):         try:             return {'Location': data[api_settings.URL_FIELD_NAME]}         except (TypeError, KeyError):             return {} 

There are a few SO posts that that also use this method. Like this, this, and this. But I still can't figure out where the implementation is.

like image 346
k-mad Avatar asked Jul 04 '16 05:07

k-mad


People also ask

What is QuerySet in Django REST framework?

The root QuerySet provided by the Manager describes all objects in the database table. Usually, though, you'll need to select only a subset of the complete set of objects. The default behavior of REST framework's generic list views is to return the entire queryset for a model manager.

What are Viewsets in Django REST framework?

After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Django REST framework allows you to combine the logic for a set of related views in a single class, called a ViewSet .

How does Django REST framework work?

REST is a loosely defined protocol for listing, creating, changing, and deleting data on your server over HTTP. The Django REST framework (DRF) is a toolkit built on top of the Django web framework that reduces the amount of code you need to write to create REST interfaces.


1 Answers

CreateModelMixin along with all other mixin classes (Eg. ListModelMixin, UpdateModelMixin etc) are defined in rest_framework/mixins.py file.

These mixin classes provide all the basic CRUD operations on a model. You just need to define a serializer_class and queryset in your generic view to perform all these operations. DRF has separated out these common functionality in separate mixin classes so that they can be injected/mixed-in in a view and used as and when required.

In the DRF source code, there's a get_serializer method. It wasn't inherited from object and it's not a method in the CreateModelMixin class. Where does this method come from?

In the GenericAPIView, get_serializer method is defined. The combination of different mixin classes along with GenericAPIView class provide us different generic views for different use cases.

class GenericAPIView(views.APIView):     """     Base class for all other generic views.     """      def get_serializer(self, *args, **kwargs):         """         Return the serializer instance that should be used for validating and         deserializing input, and for serializing output.         """         serializer_class = self.get_serializer_class()         kwargs['context'] = self.get_serializer_context()         return serializer_class(*args, **kwargs) 

Other generic views then inherit the relevant mixin along with GenericAPIView.

Eg. CreateAPIView inherit the CreateModelMixin along with GenericAPIView to provide create-only endpoints.

# rest_framework/generics.py class CreateAPIView(mixins.CreateModelMixin,                     GenericAPIView):     ... 
like image 73
Rahul Gupta Avatar answered Sep 19 '22 14:09

Rahul Gupta