Django Rest Framework自定义响应消息 [英] Django Rest Framework custom response message

查看:364
本文介绍了Django Rest Framework自定义响应消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Django Rest Framework响应消息,我有两个问题

I have two questions about Django Rest Framework response message

1。

使用 generics.ListCreateAPIView RetrieveDestroyAPIView 时,通常返回资源

When use generics.ListCreateAPIView or RetrieveDestroyAPIView , usually return a resource

例如,使用POST方法
调用/ map /结果将类似于一个对象:

For example ,call /map/ with POST Method The result will like a object :

{
    "x_axis": "23",
    "y_axis": "25",
    "map_id": 1,
}

我想知道是否可以将此消息编辑为以下自定义格式?

I want to know can I edit this message to custom like below?

{ Success: msg blablabla}

2。

当我使用 serializers.ValidationError 时,
如果使用 raise serializers.ValidationError( map_id不存在)
,我可以写我的自定义消息
响应消息将是

When I use serializers.ValidationError , I can write my custom message if I use raise serializers.ValidationError("map_id does not exist") The response message will be

{"map_id":["map_id does not exist"]}

我可以像下面这样自定义此部分吗?

Can I edit this part to custom like below?

{"FAIL":"map_id does not exist"}

我想知道这一点,因为前端不希望使用这种格式,
他们喜欢:

I want to know this because front-end don't want this format, They like :

{"Success":"msg blablabla"}
{"Fail":"msg blablabla"}
{"USERNAME_DUPLICATE":1001}
{"FIELD_REQUIRED":1002}

因此,它们可以更方便地告诉用户操作错误的原因?

So they can be more convenient to tell user the operate error cause ?

推荐答案

1在视图上覆盖create方法,并放置如下内容:

1 Overwrite the create method on the view and put something like this:

def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response({"Success": "msb blablabla"}, status=status.HTTP_201_CREATED, headers=headers)

2在代码ab中ove,将 raise_exception 更改为 False ,如果序列化程序无效,则返回您想要的任何内容。即:

2 In the code above, change raise_exception to False and return whatever you want if the serializer is not valid. i.e.:

def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        if not serializer.is_valid(raise_exception=False):
            return Response({"Fail": "blablal", status=status.HTTP_400_BAD_REQUEST)

        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response({"Success": "msb blablabla"}, status=status.HTTP_201_CREATED, headers=headers)

您正在使用CBV,因此可以创建扩展DRF类的自定义泛型类,和DRY

You are using CBV so you'll be able to create your custom generic classes that extends DRF's classes, and DRY

但是,我想说您不应该在响应中添加成功或失败 ...如果http代码是2xx用户会知道没问题,当请求有问题时为4xx,在代码(或服务器)上出现错误时为5xx,您无需在响应的正文中重复该信息,只需使用HT TP状态代码

however, I'd say that you shouldn't add "success" or "fail" in your responses... if the http code is 2xx the user will know it was OK, 4xx when the request has a problem and 5xx when there was a error on your code (or the server), you don't need to repeat that information on the body of your response, just use the HTTP status codes

希望这会有所帮助

这篇关于Django Rest Framework自定义响应消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆