gpt4 book ai didi

django - 如何在 UpdateAPIView 中返回自定义 JSON 响应?

转载 作者:行者123 更新时间:2023-12-02 09:52:58 24 4
gpt4 key购买 nike

我试图在用户提交后完成更新数据后返回一些自定义消息。我如何在 UpdateApiView 中执行此操作?

class DetailUpdateView(generics.UpdateAPIView):
permission_classes = (permissions.IsAuthenticated,)
lookup_field = "pk"
serializer_class = DetailSerializers

def get_queryset(self):
data = Data.objects.all()
return data

我想要这样的回复:

{
"message": "success",
"status": 200,
"details": [
{
"id": 2,
"DOB": "2012-10-21",
"cycle_length": "10",
"category": "2",
"child_dob": null,
"Period": "10",
"Last_cycle": "2012-10-21",
"userid": 2
},

最佳答案

您可以从 UpdateAPIView 重写 update,如下所示:

class DetailUpdateView(generics.UpdateAPIView):
permission_classes = (permissions.IsAuthenticated,)
lookup_field = "pk"
serializer_class = DetailSerializers

def get_queryset(self):
data = Data.objects.all()
return data

def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
result = {
"message": "success",
"details": serializer.data,
"status": 200,

}
return Response(result)

关于django - 如何在 UpdateAPIView 中返回自定义 JSON 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59609826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com