Пользовательское сообщение об ошибке не выдается при проверке модели

Я создал пользовательское сообщение об ошибке max_length на поле title модели Question. Однако, когда форма QuestionForm проверяется, она выдает стандартное сообщение об ошибке max_length: 'Ensure this value has at most 55 characters (it has 60).'.

Почему при проверке формы используется сообщение об ошибке по умолчанию?

AssertionError: ValidationError(['Ensure this value has at most 55 characters (it has 60).']) != 'The title of your question is too long'

class TestQuestionSubmissionTitleField(TestCase):
    """Verify that an error is raised when the title exceeds
    the maximum character limit."""

    @classmethod
    def setUpTestData(cls):
        user = get_user_model().objects.create_user("TestUser")
        profile = Profile.objects.create(user=user)
        msg = "This is a very very very very very very very very long title"
        data = {
            'title': msg,
            'body': "The context to this post doesn't explain alot does it"
        }
        cls.form = QuestionForm(data)

    def test_invalid_question_title_length(self):
        import pdb; pdb.set_trace()
        self.assertFalse(self.form.is_valid())
        self.assertTrue(self.form.has_error("title"))
        self.assertEqual(
            self.form.errors.as_data()['title'][0],
            "The title of your question is too long"
        )

from django.forms import ModelForm, CharField
from django.forms.widgets import TextInput, Textarea

from .models import Question


class QuestionForm(ModelForm):

    body = CharField(
        widget=Textarea(attrs={"class": "question_input_field"}),
        min_length=50,
        help_text="Clarify your question with as much detail as possible",
        error_messages={
            'required': "Elaborate on your question",
            'min_length': "Add more info to your question"
        }
    )

    tags = CharField(widget=TextInput(
        attrs={"class": "question_input_field"}
    ), required=False, help_text="Add up to 4 tags for your question")


    class Meta:
        model = Question
        fields = ['title', 'body', 'tags']


class Question(Post):

    title = CharField(
        max_length=55, unique_for_date="date",
        help_text="Concisely state the problem you're having",
        error_messages={
            "max_length": "The title of your question is too long"
        }
    )
    tags = ManyToManyField(
        'Tag', related_name="questions", related_query_name="question"
    )
    views = IntegerField(default=0)
    objects = Manager()
    postings = QuestionSearchManager()
Вернуться на верх