This commit is contained in:
Emilio Dorigatti 2017-06-06 20:18:02 +00:00 committed by GitHub
commit c8049b5ae9
2 changed files with 27 additions and 0 deletions

View File

@ -227,6 +227,20 @@ As with `ModelViewSet`, you'll normally need to provide at least the `queryset`
Again, as with `ModelViewSet`, you can use any of the standard attributes and method overrides available to `GenericAPIView`. Again, as with `ModelViewSet`, you can use any of the standard attributes and method overrides available to `GenericAPIView`.
## MultiSerializerViewSet
The `MultiSerializerViewSet` is an extension to the `ModelViewSet` which adds the possibility of using more than one serializer depending on the action currently taken. When the action is not found in the `serializers` dictionary, the provided default serializer will be used instead.
#### Example
class UserGroupsViewSet(MultiSerializerViewSet):
model = models.Groups
serializers = {
'list': myserializers.GroupListSerializer,
'detail': myserializers.GroupDetailSerializer,
'default': serializers.HyperlinkedModelSerializer,
}
# Custom ViewSet base classes # Custom ViewSet base classes
You may need to provide custom `ViewSet` classes that do not have the full set of `ModelViewSet` actions, or that customize the behavior in some other way. You may need to provide custom `ViewSet` classes that do not have the full set of `ModelViewSet` actions, or that customize the behavior in some other way.

View File

@ -154,3 +154,16 @@ class ModelViewSet(mixins.CreateModelMixin,
`partial_update()`, `destroy()` and `list()` actions. `partial_update()`, `destroy()` and `list()` actions.
""" """
pass pass
class MultiSerializerViewSet(ModelViewSet):
"""
A ModelViewSet which allows to use different serializers according to
the currently taken action.
"""
serializers = {
'default': None,
}
def get_serializer_class(self):
return self.serializers.get(self.action,
self.serializers['default'])