Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Display a custom error message for admin validation error

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?

like image 538
Nick Heiner Avatar asked Feb 23 '11 19:02

Nick Heiner


People also ask

How do I increase validation error in Django admin?

To create such an error, you can raise a ValidationError from the clean() method. For example: from django import forms from django. core.

How do I change the admin text in Django?

To change the admin site header text, login page, and the HTML title tag of our bookstore's instead, add the following code in urls.py . The site_header changes the Django administration text which appears on the login page and the admin site. The site_title changes the text added to the <title> of every admin page.


1 Answers

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",]})

I think the admin expects any number of validation messages to be associated with each field on a form.

like image 169
meshantz Avatar answered Sep 24 '22 00:09

meshantz