我有一个ListField序列化器,如果为空,它应该显示相应的消息:
class CreatePostSerializer(serializers.ModelSerializer):
user = serializers.PrimaryKeyRelatedField(
read_only=True,
)
topics = serializers.ListField(
child=serializers.CharField(
max_length=50,
error_messages={'max_length': 'Each tag should be no more than 50 characters.'}
),
max_length=3,
write_only=True,
error_messages={'required': 'You need to add at least one tag to your question.'}
)
class Meta:
model = Question
fields = ('user', 'status', 'topics')
当列表为空时,未引发错误消息:You need to add at least one tag to your question.
,而是引发了默认错误消息:`此字段为必填。”如何使其显示我的自定义错误消息代替?