Where did the self.get_serializer method appear in the Django REST Framework?

There is a get_serializer method in the DRF source code . It was not inherited from the object, and it is not a method in the CreateModelMixin class. Where does this method come from?

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

Here's more code snippet 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 several SO posts that also use this method. Like this one , this one, and this one . But I still can't figure out where the implementation is.

+6
source share
2 answers

CreateModelMixin together with all other mixin classes (e.g. ListModelMixin , UpdateModelMixin , etc.) are defined in the rest_framework/mixins.py .

These mixin classes provide all the basic operations of a CRUD model. You just need to define serializer_class and queryset in your general view to perform all of these operations. DRF has highlighted these common functions in separate mixin classes so that they can be injected / mixed in a view and used as needed.

There is a get_serializer method in the DRF source code. It was not inherited from the object, and it is not a method in the CreateModelMixin class. Where does this method come from?

GenericAPIView defines the get_serializer method. The combination of various mixin classes together with the GenericAPIView class provides us with different general 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 common views then inherit the corresponding mixin along with the GenericAPIView .

For instance. CreateAPIView inherit CreateModelMixin with GenericAPIView to provide create-only endpoints.

 # rest_framework/generics.py class CreateAPIView(mixins.CreateModelMixin, GenericAPIView): ... 
+3
source

You can see a member of the __file__ or __module__ method (if any) to find out. inspect also has getsourcefile and getsourcelines , which use data from a function code object, in particular <function>.f_code.co_filename and .co_firstline .

For example, this returns the source information for a method inherited from DictMixin :

 >>> c=ConfigParser._Chainmap() >>> f=c.__len__ >>> dump(f.__code__) #my custom function that dumps attributes <...> co_filename : /usr/lib/python2.7/UserDict.py co_firstlineno : 179 <...> #same with inspect >>> inspect.getsourcefile(f) '/usr/lib/python2.7/UserDict.py' >>> inspect.getsourcelines(f) ([' def __len__(self):\n', ' return len(self.keys())\n'], 179) >>> inspect.getsource(f) ' def __len__(self):\n return len(self.keys())\n' #same with __file__ >>> sys.modules[f.__module__].__file__ '/usr/lib/python2.7/UserDict.pyc' 
+1
source

All Articles