在ModelViewSet中返回422状态代码

dan*_*ast 5 django ember.js ember-data django-rest-framework json-api

为了与EmberData的互操作性,似乎每当发生验证错误时我需要用422(Unprocessable Entity)而不是400(Bad Request)来回复.我有两个问题:

和奖金:

  • 为什么EmberData期待422?据我所知,这不是JSONAPi规范的一部分.

Lin*_*via 6

422是WebDAV DRF的一部分,其错误代码不在DRF中.这并不妨碍您使用它.它们只是数字本身的可读版本.

一种选择是覆盖rest_framework.exceptions.ValidationError.status_code并将其设置为422.

编辑 - 更改默认错误代码:

# At the top of a views.py file, by the ends of import add:
from rest_framework.exceptions import ValidationError
ValidationError.status_code = 422
Run Code Online (Sandbox Code Playgroud)