DjangoRestFramwork how to override ModelViewSet get method
Question:
I have a model like this :
class AccountViewSet(viewsets.ModelViewSet):
"""
A simple ViewSet for viewing and editing accounts.
"""
queryset = Account.objects.all()
serializer_class = AccountSerializer
permission_classes = [IsAccountAdminOrReadOnly]
How do I override the get method so when I hit /api/accounts/8
I can add some code before returning the 8th account ?
Answers:
You can overwrite the retrieve method of your ActionViewSet.
def retrieve(self, request, *args, **kwarge):
account = self.get_object()
# write your extra codes here
serializer = self.get_serializer(instance=account)
return Response(data=serializer.data, status=status.HTTP_200_OK
return Response(
ModelViewSet
have mixins.RetrieveModelMixin
, which call when you hit /api/accounts/8
.You can override retrieve
method from it and do extra work.
class AccountViewSet(viewsets.ModelViewSet):
"""
A simple ViewSet for viewing and editing accounts.
"""
queryset = Account.objects.all()
serializer_class = AccountSerializer
permission_classes = [IsAccountAdminOrReadOnly]
def retrieve(self, request, *args, **kwargs):
#todo anything
instance = self.get_object()
serializer = self.get_serializer(instance)
return Response(serializer.data)
You can override the retrieve
method.
/api/accounts/8
is a detail api call. It uses the retrieve method.
So, If you look at the viewset code. we can find that.
class ModelViewSet(mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin,
GenericViewSet):
"""
A viewset that provides default `create()`, `retrieve()`, `update()`,
`partial_update()`, `destroy()` and `list()` actions.
"""
pass
solution
class AccountViewSet(viewsets.ModelViewSet):
"""
A simple ViewSet for viewing and editing accounts.
"""
queryset = Account.objects.all()
serializer_class = AccountSerializer
permission_classes = [IsAccountAdminOrReadOnly]
def retrieve(self, request, *args, **kwargs):
# do your stuff - start
...
# end
return super().retrieve(request, *args, **kwargs):
ref: https://github.com/encode/django-rest-framework/blob/master/rest_framework/viewsets.py
I have a model like this :
class AccountViewSet(viewsets.ModelViewSet):
"""
A simple ViewSet for viewing and editing accounts.
"""
queryset = Account.objects.all()
serializer_class = AccountSerializer
permission_classes = [IsAccountAdminOrReadOnly]
How do I override the get method so when I hit /api/accounts/8
I can add some code before returning the 8th account ?
You can overwrite the retrieve method of your ActionViewSet.
def retrieve(self, request, *args, **kwarge):
account = self.get_object()
# write your extra codes here
serializer = self.get_serializer(instance=account)
return Response(data=serializer.data, status=status.HTTP_200_OK
return Response(
ModelViewSet
have mixins.RetrieveModelMixin
, which call when you hit /api/accounts/8
.You can override retrieve
method from it and do extra work.
class AccountViewSet(viewsets.ModelViewSet):
"""
A simple ViewSet for viewing and editing accounts.
"""
queryset = Account.objects.all()
serializer_class = AccountSerializer
permission_classes = [IsAccountAdminOrReadOnly]
def retrieve(self, request, *args, **kwargs):
#todo anything
instance = self.get_object()
serializer = self.get_serializer(instance)
return Response(serializer.data)
You can override the retrieve
method.
/api/accounts/8
is a detail api call. It uses the retrieve method.
So, If you look at the viewset code. we can find that.
class ModelViewSet(mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin,
GenericViewSet):
"""
A viewset that provides default `create()`, `retrieve()`, `update()`,
`partial_update()`, `destroy()` and `list()` actions.
"""
pass
solution
class AccountViewSet(viewsets.ModelViewSet):
"""
A simple ViewSet for viewing and editing accounts.
"""
queryset = Account.objects.all()
serializer_class = AccountSerializer
permission_classes = [IsAccountAdminOrReadOnly]
def retrieve(self, request, *args, **kwargs):
# do your stuff - start
...
# end
return super().retrieve(request, *args, **kwargs):
ref: https://github.com/encode/django-rest-framework/blob/master/rest_framework/viewsets.py