姜戈管理员"Key 'category' not found in 'SpecificationParameterForm'. Choices are: help_text, name, un



当我在管理界面中查看更改列表时,我可以看到'name', 'category', 'value_type', 'help_text'字段,证明类别字段没问题。但是当我单击参数以进入更改表单时,我收到上述错误。

这是我的管理员:

@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)
....

追踪

会不会是因为它是外键? 任何想法都非常感谢。

编辑:

删除fieldsets会删除错误,但默认字段不包括category

我已经尝试使用 django 1.10.7 和 1.9.9 没有区别,因此删除了上面的评论。必须是我所做的更改:-/我已经确认category不会出现在form.base_fields

这是由SortableAdminMixin引起的,因为category位于此mixin使用的模型Metaordering属性中。

我在执行pip install -U时不小心覆盖了修改后的分支。

相关内容

最新更新