0
当我在管理界面中查看更改列表时,我可以看到'name', 'category', 'value_type', 'help_text'
字段,证明类别字段没问题。但是当我点击一个参数来到变化形式时,我得到了上述错误。django admin在'SpecificationParameterForm'中找不到'Key'类别,选项有:help_text,name,units,value_type。“
这里是我的管理:
@admin.register(SpecificationParameter)
class SpecificationParameterAdmin(SortableAdminMixin, admin.ModelAdmin):
"""
For administering Specification Parameters
"""
# Fields shown in lists
list_display = ('name', 'category', 'value_type', 'help_text')
list_per_page = 20
# related field needs __ due to foreign key
search_fields = ['name', 'category__name']
# Change 'Save and add another' to 'Save as new' to easily create similar entries
save_as = True
# django 1.10 will default to staying on the page after creating a new one; redirects in 1.9 :(
# for this purpose, show the id
readonly_fields = ('id',)
# Modify the layout of the form, put the optional choices last
fieldsets = (
(None, {'fields': (('name', 'id'), 'category', 'units', 'help_text')}),
(_('Type'), {'fields': ('value_type',)}),
)
inlines = [SpecificationValueChoiceAdminInline]
def get_inline_instances(self, request, obj=None):
"""
Override to dynamically display choices if multiple choice or checkbox
"""
instances = []
for inline in self.inlines:
if inline == SpecificationValueChoiceAdminInline:
if obj and obj.value_type in (tup[0] for tup in SpecificationParameter.VALUE_TYPES[1][1]):
# for changes and not adds
instances += [inline(self.model, self.admin_site)]
else:
instances += [inline(self.model, self.admin_site)]
return instances
和相关模型片断:
class SpecificationParameter(models.Model):
"""
The fields required by parameters in the specification of a product.
"""
# _() Provides the name as a translation
name = models.CharField(_("specification parameter name (public)"), unique=True, max_length=50,
help_text=_("Be as specific as you can, for example: Minimum DC Voltage"))
# All parameters in a category need to be removed manually before it will allow you to delete the cetegory
category = models.ForeignKey(SpecificationCategory, verbose_name=_("parameter category"), on_delete=models.PROTECT,
help_text=_("Add a new or select an existing parameter section"))
help_text = models.CharField(_("help text"), max_length=160, help_text=_("Specify any additional information useful to the staff entering values"), blank=True)
....
难道是因为它是一个ForeignKey? 任何想法非常赞赏。
编辑:
删除fieldsets
删除错误,但默认字段不包括category
。
我试过用django 1.10.7和1.9.9没有区别,所以删除了上面的注释。一定要改变我做: - /我已确认category
不会出现在form.base_fields
请显示完整的回溯和'SpecificationParameter'模型。 – Alasdair
请注意,更改'self.inlines'不是线程安全的。重写['get_inline_instances'](https:// docs.djangoproject.com/en/1.11/ref/contrib/admin /#django.contrib.admin.ModelAdmin.get_inline_instances')会更好。 – Alasdair
我可以看到一个异常,那就是help_text,你有这个名字的字段吗?否则你的模型有问题,django如何将name字段中定义的help_text视为另一个字段? – Exprator