在admin中对list_editable字段进行自定义验证 [英] Custom validation in admin for list_editable fields
问题描述
我有一个带有自定义验证的管理表单。一些表单字段通过list_editable显示在列表视图中。当我通过列表视图修改这些字段时,自定义验证不会生效。但是,当我使用常规更改表单时,它确实起作用。因此,问题是如何验证通过 change_list页面所做的更改。
I have a admin form with custom validation. Some of the form fields are displayed in the list view via list_editable. When I modify these fields via the list view the custom validation does not kick in. It does work when I use the regular change form, though. So the question is how do I validate changes done via the "change_list" page.
代码可能更清晰
class ProjectForm(ModelForm):
class Meta:
model = Project
def clean(self):
print "validating!"
data = self.cleaned_data
if data.get('on_frontpage') and not data.get('frontpage_image'):
raise ValidationError('To put a project on the frontpage you must \
specify a "Frontpage image" first.')
return data
class ProjectAdmin(AdminImageMixin, DisplayableAdmin, SortableAdmin):
form = ProjectForm
...
list_editable = ("status", "on_frontpage",)
list_display = ("title", "status", "on_frontpage")
谢谢!
推荐答案
找到了它。可以通过覆盖ModelAdmin中的 get_changelist_formset方法来指定 change_list页面上使用的表单:
https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py#L524
Found it. One can specify the form used on the "change_list" page by overriding "get_changelist_formset" method in ModelAdmin: https://code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py#L524
这篇关于在admin中对list_editable字段进行自定义验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!