【发布时间】:2021-02-27 07:46:33
【问题描述】:
我目前正在学习 Django Rest Framework,但目前遇到了一个我无法解决的问题。 我给你举个例子,只是为了让图片更清晰。
from rest_framework.exceptions import PermissionDenied
from rest_framework.permissions import BasePermission
class CheckAuthentication(BasePermission):
def has_permission(self, request, view):
authenticated = request.auth
if not authenticated:
raise PermissionDenied(
{
"exception_data": None
}
)
return True
好的,所以在上面的例子中,我想响应如下 JSON
{
"exception_data": null
}
但我得到的不是它,而是
{
"exception_data": "None"
}
有什么方法可以得到想要的 JSON 吗??
【问题讨论】:
-
null不是 Python 常量。 -
@Prune 我知道,但是每当我们在响应 JSON 中返回 None 作为值时,它总是被转换为 null,因为它是 None 的对应关系
标签: python django exception django-rest-framework