Mil*_*ano 2 python django django-rest-framework
我的模型中有字段refundable
和。refundable_price
我需要确定没有refundable_price
,None
以防refundable
万一True
。
因为我想要它无处不在,所以我重写了SubOffer.clean
方法:
from django.core.exceptions import ValidationError\n\ndef save(self, **kwargs):\n self.full_clean()\n super().save(**kwargs)\n\ndef clean(self):\n super().clean()\n if self.refundable and self.refundable_price is None:\n raise ValidationError("V pr\xc3\xadpade refundovate\xc4\xbenej ponuky je nutn\xc3\xa9 zada\xc5\xa5 sumu (je mo\xc5\xben\xc3\xa9 zada\xc5\xa5 aj 0)")\n
Run Code Online (Sandbox Code Playgroud)\n\n而我用的是ModelViewSet
.
class SubOfferViewSet(ModelViewSet):\n serializer_class = SubOfferSerializer\n filterset_fields = {\n # \'approved_by\': [\'exact\'],\n # \'approved_dt\': [\'gte\', \'lte\', \'gt\', \'lt\'],\n }\n\n def get_queryset(self):\n return SubOffer.objects.all()\n
Run Code Online (Sandbox Code Playgroud)\n\n奇怪的是,当我发送POST
到时,如果 中存在错误,ViewSet
它会返回500
而不是JSON
错误Suboffer.clean
。其他错误工作正常。
AJAX
我用的时候和用的时候是一样的DRF API Viewer
。
Pau*_*eri 12
处理所有验证错误(以及您可能想要的任何其他错误)的一种简洁方法是使用一个EXCEPTION_HANDLER
将 Django 转换
ValidationError
为 DRF 的自定义方法。
看:
from django.core.exceptions import ValidationError as DjangoValidationError
from rest_framework.exceptions import ValidationError as DRFValidationError
from rest_framework.serializers import as_serializer_error
from rest_framework.views import exception_handler as drf_exception_handler
def exception_handler(exc, context):
if isinstance(exc, DjangoValidationError):
exc = DRFValidationError(as_serializer_error(exc))
return drf_exception_handler(exc, context)
"""
In settings:
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'path.to_module.drf.exception_handler',
}
"""
Run Code Online (Sandbox Code Playgroud)
来源:https ://gist.github.com/twidi/9d55486c36b6a51bdcb05ce3a763e79f
默认情况下仅使用 DRF 句柄APIException
(请参阅源代码)。由于您出现了Django's
ValidationError instead of DRF's
Validation` 错误,因此该处理程序返回 None。
因此,要解决此问题,您可以使用ValidationError
DRF:
from rest_framework.exceptions import ValidationError
Run Code Online (Sandbox Code Playgroud)
from rest_framework.views import exception_handler
from django.core.exceptions import ValidationError
def custom_exception_handler(exc, context):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
if response is None and isinstance(exc, ValidationError):
return Response(status=400)
return response
Run Code Online (Sandbox Code Playgroud)