我可以看到在使用表单时如何向字段添加错误消息,但是模型表单呢? 这是我的测试模型: class Author(models.Model): first_name = models.CharField(max_length=125) last_name = models.CharField(max_length=125) c
这是我的测试模型:
class Author(models.Model): first_name = models.CharField(max_length=125) last_name = models.CharField(max_length=125) created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True)
我的模特形式:
class AuthorForm(forms.ModelForm): class Meta: model = Author
字段:first_name和last_name上的错误消息是:
This field is required
如何在模型表单中更改它?
对于简单的情况,你可以 specify custom error messagesclass AuthorForm(forms.ModelForm): first_name = forms.CharField(error_messages={'required': 'Please let us know what to call you!'}) class Meta: model = Author