[Answered ]-Changing the required field text on a modelform in django

2👍

For simple cases you can specify custom error messages in your ModelForm class.

class UserForm(forms.ModelForm):
    first_name = forms.CharField(error_messages={'required': 'test'})

    class Meta:
        model = User
        fields = ('first_name', 'last_name',...)

You could also override the __init__ of the class.

class UserForm(forms.ModelForm):
    class Meta:
        model = User

    def __init__(self, *args, **kwargs):
        super(UserForm, self).__init__(*args, **kwargs)

        self.fields['first_name'].error_messages = {'required': 'test'}
        ...

Leave a comment