Create Custom Error Messages with Model Forms

后端 未结 7 2037
一向
一向 2020-11-28 06:56

I can see how to add an error message to a field when using forms, but what about model form?

This is my test model:

class Author(models.Model):
             


        
7条回答
  •  半阙折子戏
    2020-11-28 07:25

    For simple cases, you can specify custom error messages

    class AuthorForm(forms.ModelForm):
        first_name = forms.CharField(error_messages={'required': 'Please let us know what to call you!'})
        class Meta:
            model = Author
    

提交回复
热议问题