django - TypeError: 'list' 对象不可调用 | DRF 异常处理
问题描述
我正在尝试将 Django 模型 ValidationError 解析为 DRF ValidationError。但我不断得到以下输出:
TypeError: 'list' object is not callable
这是模型功能: 为避免重复:我将粘贴上一个具有模型功能的问题的链接。
这是我的自定义异常处理程序:
import logging
from django.core.exceptions import ValidationError as DjangoValidationError
from rest_framework.exceptions import ValidationError
LOG = logging.getLogger(__name__)
def transform_exception(exception):
"""Transform model validation errors into an equivalent \
DRF ValidationError.
After reading the references, you may decide not to use this.
References:
https://www.kye.id.au/blog/understanding-django-rest-framework-model-full-clean/
https://www.dabapps.com/blog/django-models-and-encapsulation/
"""
if isinstance(exception, DjangoValidationError):
if hasattr(exception, "message_dict"):
detail = exception.message_dict
elif hasattr(exception, "message"):
detail = exception.message
elif hasattr(exception, "messages"):
detail = exception.messages
else:
LOG.error("BAD VALIDATION MESSAGE: %s", exception)
exception = ValidationError(detail=detail)
return exception
这是我的错误日志:
我哪里错了?
解决方案
推荐阅读
- selenium-webdriver - org.openqa.selenium.SessionNotCreatedException: session not created: 这个版本的 ChromeDriver 只支持 Chrome 版本 79
- javascript - 如何在 Javascript 中将 PNG 转换为 SVG
- java - 从另一个类调用静态变量到可运行
- html - Bootstrap 4 确实有问题(导航栏和弹性对齐)
- python - Heroku Django 应用程序没有服务,因为 ModuleNotFoundError 崩溃:
(*不是需求模块*)? - selenium - 如何使用 selenium 点击这个按钮?
- javascript - onclick javascript函数不适用于div元素
- css - 我的 css first-child 伪选择器以相反的方式工作
- c - 虽然循环在C中被跳过
- list - F#关于打字的问题,float*float类型与'float'类型不匹配