Django Rest Framework 如何覆盖 ModelViewSet 的 get 方法

huangapple go评论97阅读模式
英文:

DjangoRestFramwork how to override ModelViewSet get method

问题

我有一个像这样的模型:

  1. class AccountViewSet(viewsets.ModelViewSet):
  2. """
  3. 用于查看和编辑帐户的简单视图集。
  4. """
  5. queryset = Account.objects.all()
  6. serializer_class = AccountSerializer
  7. permission_classes = [IsAccountAdminOrReadOnly]

我该如何重写get方法,以便当我访问/api/accounts/8时,在返回第8个帐户之前可以添加一些代码?

英文:

I have a model like this :

  1. class AccountViewSet(viewsets.ModelViewSet):
  2. """
  3. A simple ViewSet for viewing and editing accounts.
  4. """
  5. queryset = Account.objects.all()
  6. serializer_class = AccountSerializer
  7. 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 ?

答案1

得分: 1

ModelViewSet包含mixins.RetrieveModelMixin,当你访问/api/accounts/8时会调用它。你可以重写它的retrieve方法并进行额外的工作。

  1. class AccountViewSet(viewsets.ModelViewSet):
  2. """
  3. 用于查看和编辑帐户的简单ViewSet。
  4. """
  5. queryset = Account.objects.all()
  6. serializer_class = AccountSerializer
  7. permission_classes = [IsAccountAdminOrReadOnly]
  8. def retrieve(self, request, *args, **kwargs):
  9. # 做任何需要的工作
  10. instance = self.get_object()
  11. serializer = self.get_serializer(instance)
  12. return Response(serializer.data)
英文:

ModelViewSet have mixins.RetrieveModelMixin, which call when you hit /api/accounts/8.You can override retrieve method from it and do extra work.

  1. class AccountViewSet(viewsets.ModelViewSet):
  2. """
  3. A simple ViewSet for viewing and editing accounts.
  4. """
  5. queryset = Account.objects.all()
  6. serializer_class = AccountSerializer
  7. permission_classes = [IsAccountAdminOrReadOnly]
  8. def retrieve(self, request, *args, **kwargs):
  9. #todo anything
  10. instance = self.get_object()
  11. serializer = self.get_serializer(instance)
  12. return Response(serializer.data)

答案2

得分: 0

你可以覆盖ActionViewSet的retrieve方法。

  1. def retrieve(self, request, *args, **kwarge):
  2. account = self.get_object()
  3. # 在这里编写额外的代码
  4. serializer = self.get_serializer(instance=account)
  5. return Response(data=serializer.data, status=status.HTTP_200_OK
英文:

You can overwrite the retrieve method of your ActionViewSet.

  1. def retrieve(self, request, *args, **kwarge):
  2. account = self.get_object()
  3. # write your extra codes here
  4. serializer = self.get_serializer(instance=account)
  5. return Response(data=serializer.data, status=status.HTTP_200_OK
  6. return Response(

答案3

得分: 0

retrieve 方法可以被覆盖。

/api/accounts/8 是一个详细的 API 调用。它使用了 retrieve 方法。

所以,如果你看一下 viewset 代码,我们可以找到以下内容。

  1. class ModelViewSet(mixins.CreateModelMixin,
  2. mixins.RetrieveModelMixin,
  3. mixins.UpdateModelMixin,
  4. mixins.DestroyModelMixin,
  5. mixins.ListModelMixin,
  6. GenericViewSet):
  7. """
  8. 提供默认的`create()``retrieve()``update()`
  9. `partial_update()``destroy()``list()`操作的 viewset
  10. """
  11. pass

解决方案

  1. class AccountViewSet(viewsets.ModelViewSet):
  2. """
  3. 用于查看和编辑账户的简单 ViewSet
  4. """
  5. queryset = Account.objects.all()
  6. serializer_class = AccountSerializer
  7. permission_classes = [IsAccountAdminOrReadOnly]
  8. def retrieve(self, request, *args, **kwargs):
  9. # 进行你的操作 - 开始
  10. ...
  11. # 结束
  12. return super().retrieve(request, *args, **kwargs):

参考链接: https://github.com/encode/django-rest-framework/blob/master/rest_framework/viewsets.py

英文:

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.

  1. class ModelViewSet(mixins.CreateModelMixin,
  2. mixins.RetrieveModelMixin,
  3. mixins.UpdateModelMixin,
  4. mixins.DestroyModelMixin,
  5. mixins.ListModelMixin,
  6. GenericViewSet):
  7. """
  8. A viewset that provides default `create()`, `retrieve()`, `update()`,
  9. `partial_update()`, `destroy()` and `list()` actions.
  10. """
  11. pass

solution

  1. class AccountViewSet(viewsets.ModelViewSet):
  2. """
  3. A simple ViewSet for viewing and editing accounts.
  4. """
  5. queryset = Account.objects.all()
  6. serializer_class = AccountSerializer
  7. permission_classes = [IsAccountAdminOrReadOnly]
  8. def retrieve(self, request, *args, **kwargs):
  9. # do your stuff - start
  10. ...
  11. # end
  12. return super().retrieve(request, *args, **kwargs):

ref: https://github.com/encode/django-rest-framework/blob/master/rest_framework/viewsets.py

huangapple
  • 本文由 发表于 2023年2月14日 01:22:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定