Skip to content

Instantly share code, notes, and snippets.

@gonz
Created April 7, 2014 20:16
Show Gist options
  • Save gonz/10044002 to your computer and use it in GitHub Desktop.
Save gonz/10044002 to your computer and use it in GitHub Desktop.
MultiSerializerViewSetMixin
class MultiSerializerViewSetMixin(object):
def get_serializer_class(self):
"""
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 MyViewSet(ViewSet):
serializer_class = MyDefaultSerializer
serializer_action_classes = {
'list': MyListSerializer,
'my_action': MyActionSerializer,
}
@action
def my_action:
...
If there's no entry for that action then just fallback to the regular
get_serializer_class lookup: self.serializer_class, DefaultSerializer.
"""
try:
return self.serializer_action_classes[self.action]
except KeyError:
return super(MultiSerializerViewSetMixin, self).get_serializer_class()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment