如何在Django中将不可编辑字段添加到自定义管理表单中 [英] How do you add a non-editable field to a custom admin form in Django
本文介绍了如何在Django中将不可编辑字段添加到自定义管理表单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试向自定义管理表单中添加 editable = False
字段,但出现错误:
I am trying to add an editable=False
field to a custom admin form, but I am getting an error:
django.core.exceptions.FieldError: 'help_num' cannot be specified for
Investigation model form as it is a non-editable field
这是真的,在我的模型中,我将其设置为:
This is true, in my model I have it set as such:
models.py
models.py
help_num = models.CharField(max_length=17, unique=True, default=increment_helpdesk_number, editable=False)
forms.py
class HelpDeskModelForm(forms.ModelForm):
class Meta:
model = HelpDesk
fields = [
"help_num",
"help_types",
...
"help_summary"
]
admin.py
class HelpDeskModelAdmin(admin.ModelAdmin):
readonly_fields=('help_num',)
form = HelpDeskModelForm
我在admin.py中添加了只读
,但是仍然出现错误。我在做什么错了?
I added the readonly
to admin.py, but am still getting the error. What am I doing wrong?
推荐答案
您需要从您的班级表格字段列表中删除不可编辑的字段:
You need to remove the non-editable field from your class form list of fields :
class HelpDeskModelForm(forms.ModelForm):
class Meta:
model = HelpDesk
fields = [
#"help_num",
"help_types",
...
"help_summary"
]
并像以前一样在ModelAdmin中保留只读字段:
And keep the read-only fields in the ModelAdmin like you did :
class HelpDeskModelAdmin(admin.ModelAdmin):
readonly_fields=('help_num',)
form = HelpDeskModelForm
这篇关于如何在Django中将不可编辑字段添加到自定义管理表单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文