首页 > 解决方案 > TypeError: 'list' 对象不可调用 | DRF 异常处理

问题描述

我正在尝试将 Django 模型 ValidationError 解析为 DRF ValidationError。但我不断得到以下输出:

TypeError: 'list' object is not callable

这是模型功能: 为避免重复:我将粘贴上一个具有模型功能的问题的链接。

模型.py

这是我的自定义异常处理程序:

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

这是我的错误日志:

服务器日志

我哪里错了?

标签: djangodjango-modelsdjango-rest-framework

解决方案


推荐阅读