0

Can I change the error message from the model for a class-based view? the following is not working, it always gives the default message from Django.

Model:

class Child(models.Model):
   name = models.CharField(max_length=100, error_messages={ 'blank': 'cannot be blank or null', 'null': 'cannot be blank or null',})

view:

class ChildCreate(CreateView):
  model = Child
  fields = '__all__'
  success_url = reverse_lazy('children-list')
1
  • you have to create a ModelForm in forms.py and add that form in your view form_class=<your_form_class_name> add the error_message attribute in forms it will work Commented Jul 5, 2018 at 11:53

1 Answer 1

1

From the docs:

Error messages defined at the form field level or at the form Meta level always take precedence over the error messages defined at the model field level.

So you can create model form and add required error message there:

class ChildForm(forms.ModelForm):
    use_required_attribute = False

    class Meta:
        model = models.Child
        fields = '__all__'
        error_messages = {'name': {'required': 'cannot be blank or null'}} 

class ChildCreate(CreateView):
    model = Child
    form_class = forms.ChildForm
    success_url = reverse_lazy('children-list')
Sign up to request clarification or add additional context in comments.

2 Comments

still shows me this pop message: Please fill out this field.
@Yousef to disable popup use use_required_attribute = False form's attribute. See update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.