我正在与 djoser 合作,在阅读文档后,我想从 Userviewset 更改代码的某些部分。
我想覆盖def me(self, request, *args, **kwargs)
和自定义它。对于 get 请求,我不想返回模型的所有字段,而是只返回Username和Profile Picture。
我试着做from djoser.views import UserViewSet
。然后,
class UserViewSet(viewsets.ModelViewSet):
# from djoser docs which I want to update
@action(["get", "put", "patch", "delete"], detail=False)
def me(self, request, *args, **kwargs):
self.get_object = self.get_instance
if request.method == "GET":
return self.retrieve(request, *args, **kwargs)
elif request.method == "PUT":
return self.update(request, *args, **kwargs)
elif request.method == "PATCH":
return self.partial_update(request, *args, **kwargs)
elif request.method == "DELETE":
return self.destroy(request, *args, **kwargs)
但我想知道这会起作用,因为既没有retrieve
也没有get_instance
提到。有没有更短的正确方法来做到这一点?