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

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

How to return custom JSON response in UpdateAPIView?

问题

I was trying to return some custom message after the user completes updating data after submission. How can I do this in the 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

I want a response something like this:

{
    "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
        }
    ]
}
英文:

I was trying to return some custom message after user complete updated data after submit. How can i do this in 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

I want response something like this:

{
"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
    },

答案1

得分: 4

你可以像这样覆盖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)

注意:代码部分不需要翻译。

英文:

you can override update from UpdateAPIView like this:

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)

huangapple
  • 本文由 发表于 2020年1月6日 17:46:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609826.html
匿名

发表评论

匿名网友

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

确定