[Django]-Django, Models & Forms: replace "This field is required" message

49👍

class MyForm(forms.ModelForm):
    class Meta:
            model = MyModel

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['name'].error_messages = {'required': 'custom required message'}

        # if you want to do it to all of them
        for field in self.fields.values():
            field.error_messages = {'required':'The field {fieldname} is required'.format(
                fieldname=field.label)}

-1👍

you can change field attributes at runtime, in the __init__ method.

👤Jerzyk

Leave a comment