Guest User

Untitled

a guest
Oct 17th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. class MultiSerializerViewSetMixin(object):
  2. def get_serializer_class(self):
  3. """
  4. Look for serializer class in self.serializer_action_classes, which
  5. should be a dict mapping action name (key) to serializer class (value),
  6.  
  7. i.e.:
  8. class MyViewSet(MultiSerializerViewSetMixin, ViewSet):
  9. serializer_class = MyDefaultSerializer
  10. serializer_action_classes = {
  11. 'list': MyListSerializer,
  12. 'my_action': MyActionSerializer,
  13. }
  14. @action
  15. def my_action:
  16. ...
  17. If there's no entry for that action then just fallback to the regular
  18. get_serializer_class lookup: self.serializer_class, DefaultSerializer.
  19. """
  20. try:
  21. return self.serializer_action_classes[self.action]
  22. except (KeyError, AttributeError):
  23. return super(MultiSerializerViewSetMixin,
  24. self).get_serializer_class()
Add Comment
Please, Sign In to add comment