How to override ArrayField default error messages in template?


April 2019


12 time


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 TopicForm Meta class but had no success

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

Here is my 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

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