Arp*_*thi 5 django django-rest-framework
DRF 给出的默认验证错误消息是密钥和消息列表。将此格式自定义为文本格式的最佳方法是什么。例如。
这是默认格式。
{
"message": {
"phone": [
"customer with this phone already exists."
],
"email": [
"customer with this email already exists."
],
"tenant_id": [
"customer with this tenant id already exists."
]
},
"success": false,
"error": 1
}
Run Code Online (Sandbox Code Playgroud)
这是我想要的东西。
{
"message": "customer with this phone already exists, customer with this
email already exists, customer with this tenant id already exists"
"success": false,
"error": 1
}
Run Code Online (Sandbox Code Playgroud)
通常的做法是显示各个字段的验证消息,而不是提供单个常规消息。所以 DRF 的默认行为就是遵循这个约定。
为了实现你想要的,你需要为序列化器创建对象级验证http://www.django-rest-framework.org/api-guide/serializers/#object-level-validation并防止字段验证的默认行为。
| 归档时间: |
|
| 查看次数: |
6188 次 |
| 最近记录: |