且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

django rest框架:通过序列化程序validate()方法设置字段级错误

更新时间:2021-11-01 09:58:12

我在页的 BaseSerializer部分中,有一个示例显示ValidationError可以在初始化时采用字典参数。

I figured it out, on this page of the documentation in the "BaseSerializer" section, there's an example that shows ValidationError can take a dictionary argument upon initialization.

如果我 raise ValidationError({'field_val1':['此字段无效']})得到我想要的JSON响应。

If I raise ValidationError({'field_val1': ['this field is not valid']}) I get the JSON response I want.