Repository URL to install this package:
|
Version:
0.0.1 ▾
|
from rest_framework.generics import GenericAPIView
from rest_framework.viewsets import ViewSetMixin
class ActionSerializerClassMixin(ViewSetMixin, GenericAPIView):
serializer_action_classes = {}
def get_serializer_class(self):
"""
A class which inherits this mixins should have variable
`serializer_action_classes`.
Look for serializer class in self.serializer_action_classes, which
should be a dict mapping action name (key) to serializer class (value),
i.e.:
class SampleViewSet(viewsets.ViewSet):
serializer_class = DocumentSerializer
serializer_action_classes = {
'upload': UploadDocumentSerializer,
'download': DownloadDocumentSerializer,
}
@action
def upload:
...
If there's no entry for that action then just fallback to the regular
get_serializer_class lookup: self.serializer_class, DefaultSerializer.
"""
return self.serializer_action_classes.get(self.action) or super().get_serializer_class()