How to override ArrayField default error messages in template?

Refresh

6 days ago

Views

12 time

2

I am trying to change the default error message Django generates for ArrayField (Specifically too many items entered error message)

If a user enters too many items to my ArrayField the following message generates in the template:

List contains 4 items, it should contain no more than 3.

I want to change this message to

You can't have more than 3 topics.

I have tried adding the following error_messages to my forms.py TopicForm Meta class but had no success

    error_messages = {
        'topic': {
            'invalid': ("You can't have more than 3 topics."),
        },

Here is my models.py file

from django.contrib.postgres.fields import ArrayField
from django.db import models

class Topic(models.Model)
    topic = ArrayField(models.CharField(max_length=20), size=3, blank=True,    null=True)

and my forms.py

from django import forms
from .models import Topic

class TopicForm(forms.ModelForm):
    class Meta:
        model = Topic

        fields = ['topic']

Would apperciate some input on this! Thank you!

0 answers