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.
source share