自定义 ModelSerializer 错误消息被忽略
Customizing ModelSerializer error message is being ignored
这是我的 UserSerializer(我使用的是默认的 Django 用户模型):
class UserSerializer(SetCustomErrorMessagesMixin, serializers.ModelSerializer):
def __init__(self, *args, **kwargs):
super(UserSerializer, self).__init__(*args, **kwargs) # call the super()
for field in self.fields: # iterate over the serializer fields
self.fields[field].error_messages['required'] = 'Enter a valid %s.'%field
self.fields[field].error_messages['null'] = 'Enter a valid %s.'%field
# class CharField(Field) errors
self.fields[field].error_messages['blank'] = 'Enter a valid %s.'%field
self.fields[field].error_messages['max_length'] = '%s cannot have more than {max_length} characters.'%field
self.fields[field].error_messages['min_length'] = '%s cannot have less than {min_length} characters.'%field
class Meta:
model = User
fields = ('username', 'password', 'email',)
问题是,当用户输入太长的用户名时,错误消息是
"Username is too long."
此错误消息来自哪里?我在上面的代码中覆盖了 "max_length" 错误消息,但它没有显示它。当我从我的 UserSerialzer 中删除这一行时:
self.fields[field].error_messages['max_length'] = '%s cannot have more than {max_length} characters.'%field
那么错误信息是:
"Ensure this field has no more than 30 characters."
这是有道理的,因为它来自此处的 CharField DRF 源代码:https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/fields.py
但是 "Username is too long." 是从哪里来的,为什么不说 "Username cannot have more than {max_length} characters." 呢?
问题似乎是 DRF 在构造过程中为字段添加了验证器,并从字段 类 复制了错误消息。
例如来自rest_framework.fields.CharField.__init__
:
if self.min_length is not None:
message = self.error_messages['min_length'].format(min_length=self.min_length)
self.validators.append(MinLengthValidator(self.min_length, message=message))
所以在您覆盖消息的那一刻,它们已经在验证器中使用了。
我认为您可以创建一个 yourapp.fields
模块,在其中子类化 DRF 序列化程序字段并覆盖它们的 default_error_messages
,如下所示:
from rest_framework import fields
class CharField(fields.CharField):
default_error_messages = {
# Your messages
}
然后只需切换从中导入字段的模块。
您可能还想覆盖 __init__
s 以在消息中添加字段名称。
这是我的 UserSerializer(我使用的是默认的 Django 用户模型):
class UserSerializer(SetCustomErrorMessagesMixin, serializers.ModelSerializer):
def __init__(self, *args, **kwargs):
super(UserSerializer, self).__init__(*args, **kwargs) # call the super()
for field in self.fields: # iterate over the serializer fields
self.fields[field].error_messages['required'] = 'Enter a valid %s.'%field
self.fields[field].error_messages['null'] = 'Enter a valid %s.'%field
# class CharField(Field) errors
self.fields[field].error_messages['blank'] = 'Enter a valid %s.'%field
self.fields[field].error_messages['max_length'] = '%s cannot have more than {max_length} characters.'%field
self.fields[field].error_messages['min_length'] = '%s cannot have less than {min_length} characters.'%field
class Meta:
model = User
fields = ('username', 'password', 'email',)
问题是,当用户输入太长的用户名时,错误消息是
"Username is too long."
此错误消息来自哪里?我在上面的代码中覆盖了 "max_length" 错误消息,但它没有显示它。当我从我的 UserSerialzer 中删除这一行时:
self.fields[field].error_messages['max_length'] = '%s cannot have more than {max_length} characters.'%field
那么错误信息是:
"Ensure this field has no more than 30 characters."
这是有道理的,因为它来自此处的 CharField DRF 源代码:https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/fields.py
但是 "Username is too long." 是从哪里来的,为什么不说 "Username cannot have more than {max_length} characters." 呢?
问题似乎是 DRF 在构造过程中为字段添加了验证器,并从字段 类 复制了错误消息。
例如来自rest_framework.fields.CharField.__init__
:
if self.min_length is not None:
message = self.error_messages['min_length'].format(min_length=self.min_length)
self.validators.append(MinLengthValidator(self.min_length, message=message))
所以在您覆盖消息的那一刻,它们已经在验证器中使用了。
我认为您可以创建一个 yourapp.fields
模块,在其中子类化 DRF 序列化程序字段并覆盖它们的 default_error_messages
,如下所示:
from rest_framework import fields
class CharField(fields.CharField):
default_error_messages = {
# Your messages
}
然后只需切换从中导入字段的模块。
您可能还想覆盖 __init__
s 以在消息中添加字段名称。