my-django/utils/json_response.py
Kai 5d9073c19c
Some checks failed
test / build (push) Has been cancelled
test / deploy (push) Has been cancelled
test / release (push) Has been cancelled
first commit
2025-03-21 17:45:04 +08:00

33 lines
871 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
@Remark: 自定义的Response返回内容
"""
from rest_framework import status as HttpStatus
from rest_framework.response import Response as RestResponse
from utils.ecode import ECode
# 1.返回单一数据
class Response(RestResponse):
def __init__(
self,
data=None,
code=HttpStatus.HTTP_200_OK,
message=None,
status=None,
template_name=None,
headers=None,
exception=False,
content_type="application/json",
):
# 如果未提供 message则通过 code 从 ECode 中获取
message = message or ECode.desc(code)
data = {
"code": code,
"data": data,
"success": code == HttpStatus.HTTP_200_OK,
"message": message,
}
super().__init__(data, status, template_name, headers, exception, content_type)