在Django管理员中,我正在尝试使用其他非列表可编辑字段中的信息来验证表单中的字段(此字段可用作list_display和list_editable字段)。
到目前为止,我已经重写了clean方法并执行了验证。当我在“表单详细信息”视图/admin/core/binddetails/48/
中但在列表显示字段/admin/core/binddetails/
中时,此方法工作正常,因为验证标准中的某些字段不能作为{{ 1}}字段
我还按照https://stackoverflow.com/a/8964559/6638281的建议修改了list_editable
get_changelist_form()
class BindDetails(models.Model):
field1 = models.BooleanField()
field2 = models.CharField(max_length=255)
field5 = models.BooleanField()
class BindDetailsForm(forms.ModelForm):
def clean(self):
cleaned_data = super(BindDetailsForm, self).clean()
field1 = cleaned_data['field1']
field5 = cleaned_data['field5']
if field1 and not field5:
raise forms.ValidationError("'field1' cannot be enabled while 'field5' is turned off!")
我想禁用class BindDetailsAdmin(admin.ModelAdmin):
model = BindDetails
form = BindDetailsForm
list_display = ('field1', 'field2')
list_editable = ('field1', 'field2')
def get_changelist_form(self, request, **kwargs):
return BindDetailsForm
,但是通过field5
页面尝试启用list_display
,则会引发验证错误。