Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. class GetSerializerClassMixin(object):
  2.  
  3. def get_serializer_class(self):
  4. """
  5. A class which inhertis this mixins should have variable
  6. `serializer_action_classes`.
  7.  
  8. Look for serializer class in self.serializer_action_classes, which
  9. should be a dict mapping action name (key) to serializer class (value),
  10. i.e.:
  11.  
  12. class SampleViewSet(viewsets.ViewSet):
  13. serializer_class = DocumentSerializer
  14. serializer_action_classes = {
  15. 'upload': UploadDocumentSerializer,
  16. 'download': DownloadDocumentSerializer,
  17. }
  18.  
  19. @action
  20. def upload:
  21. ...
  22.  
  23. If there's no entry for that action then just fallback to the regular
  24. get_serializer_class lookup: self.serializer_class, DefaultSerializer.
  25. """
  26. try:
  27. return self.serializer_action_classes[self.action]
  28. except (KeyError, AttributeError):
  29. return super().get_serializer_class()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement