如何在django rest框架中覆盖所需的error_message?

时间:2016-11-07 09:59:45

标签: django-rest-framework

我想覆盖required错误消息。对于blankempty,我可以在extra_kwargs中执行此操作,但对于required,它不起作用:

class Meta:
    extra_kwargs = {
        'enticements_info': {'error_messages': {'required': 'Test.'}},
    }

我该怎么办?

1 个答案:

答案 0 :(得分:4)

您可以通过这种方式覆盖所需的error_message。

class YourSerializer(ModelSerializer):
  my_default_errors = {
    'required': 'This field is required',
    'blank': 'This field should not blank'
    }

  enticements_info = serializers.CharField(required=True, error_messages=my_default_errors, min_length=10)

  class Meta:
    model = Your_Model
    fields = ('enticements_info')