Django:显示管理员验证错误的自定义错误消息

时间:2022-01-18 23:19:26

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.

我正在使用Django 1.2.4。我的模型有一个需要验证的字段。验证失败时,我想向用户显示自定义错误消息。模型编辑在管理界面中完成。

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.

不幸的是,所有这一切都是在admin_name页面上为field_name的值中的每个字符打印出单独的验证消息。

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

发出我想要的错误信息的正确方法是什么?

1 个解决方案

#1


15  

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

没有看,听起来像管理员正在寻找一个iterable作为field_name的值。尝试:

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.

我认为管理员希望将任意数量的验证消息与表单上的每个字段相关联。

#1


15  

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

没有看,听起来像管理员正在寻找一个iterable作为field_name的值。尝试:

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.

我认为管理员希望将任意数量的验证消息与表单上的每个字段相关联。