Django Display a custom error message for admin validation error

0 votes

I'm using Django 1.2.4. I have a model that has a field that needs to be validated. When validation fails, I'd like to display a custom error message to the user. Model editing is done in the admin interface.

This is what I'm doing currently:

def clean_fields(self, exclude=None):
    # do validation
    if problem:
        raise ValidationError({'field_name': "error message"})

Unfortunately, all this does is print out a separate validation message on the admin page for each character in the value of field_name.

What is the proper way to signal the error message I want?

Jun 12, 2020 in Python by kartik
• 37,490 points
1,841 views

1 answer to this question.

0 votes

Hello @kartik,

Without looking, it sounds like the admin is looking for an iterable as the value for field_name. Try:

raise ValidationError({'field_name': ["error message",]})

Hope it helps!!

answered Jun 12, 2020 by Niroj
• 82,560 points

Related Questions In Python

0 votes
1 answer

How to print a message or the error if a file is not found?

To print the message that file is not ...READ MORE

answered Jan 2, 2019 in Python by Omkar
• 69,090 points
230 views
0 votes
1 answer

Python error "AttributeError: incompatible shape for a non-contiguous array"

It is not always possible to change ...READ MORE

answered May 29, 2019 in Python by Imran
470 views
0 votes
1 answer
0 votes
3 answers

Python Selenium best tutorials for beginners

Hope this will help you...Python Tutorial READ MORE

answered Feb 11, 2019 in Python by aldrinjohn
• 140 points
2,340 views
0 votes
1 answer

How to load a custom JS file in Django admin home?

Hello @kartik, You can override templates/admin/index.html and add the JavaScript ...READ MORE

answered May 14, 2020 in Python by Niroj
• 82,560 points
1,999 views
0 votes
1 answer

How do I add a link from the Django admin page of one object to the admin page of a related object?

Hello @kartik, Set show_change_link to True (False by default) in your inline ...READ MORE

answered Jun 12, 2020 in Python by Niroj
• 82,560 points
1,691 views