覆盖的错误消息未在序列化器中显示

时间:2020-07-14 22:43:34

标签: django django-rest-framework

我有一个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.,而是引发了默认错误消息:`此字段为必填。”如何使其显示我的自定义错误消息代替?

0 个答案:

没有答案