如何在ModelAdmin中使用InlinePanel? [英] How to use InlinePanel in ModelAdmin?
问题描述
我正在建立一个需要使用多个子模型实例创建的模型.我希望能够在管理界面中编辑和创建此模型,所以我可以使用文档,我应该能够根据正常的 Page
类型指定字段/面板;但是,当我添加 InlinePanel
我在相关字段名称上得到了 KeyError
.
I'm setting up a model that needs to be created with a number of instances of a sub-model. I want to be able to edit and create this model in the admin interface so I'm adding it using ModelAdmin
. According to the documentation I should be able to specify the fields/panels in accordance to normal Page
types; however, when I add a InlinePanel
I get an KeyError
on the related field name.
models.py
class Application(models.Model):
# other fields....
panels = [MultiFieldPanel([
FieldPanel('applicant'),
FieldPanel('position'),
FieldPanel('cover_letter'),
FieldPanel('qualifications'),
InlinePanel('references'),
FieldPanel('draft'),
])]
class Reference(models.Model):
application = models.ForeignKey(
Application,
related_name='references',
on_delete=models.CASCADE,
blank=False,
)
# other fields....
wagtails_hooks.py
class ApplicationAdmin(ModelAdmin):
model = Application
menu_icon = 'mail'
menu_order = 400
list_display = # other fields....
modeladmin_register(ApplicationAdmin)
错误
请求网址: http://127.0.0.1:8000/admin/involvement/application/create/
Django版本:1.10.5
Django Version: 1.10.5
异常类型:KeyError
Exception Type: KeyError
异常值:引用"
异常位置:/[APPFOLDER]/venv/lib/python3.6/site-packages/wagtail/wagtailadmin/edit_handlers.py在 init 行627
Exception Location: /[APPFOLDER]/venv/lib/python3.6/site-packages/wagtail/wagtailadmin/edit_handlers.py in init, line 627
我无法确定自己做错了什么.有人能指出我正确的方向吗?
I'm having trouble determining what I did wrong. Can anybody point me in the right direction?
推荐答案
Wagtail依赖 django-modelcluster 库,以允许将父模型和子模型以表单的形式作为单个单元进行处理.为此,基本的应用程序模型必须继承 modelcluster.models.ClusterableModel
(Wagtail Page
模型作为标准模型),并且必须链接子模型通过 ParentalKey
而不是 ForeignKey
.
Wagtail relies on the django-modelcluster library to allow the parent and child models to be handled in forms as a single unit. For this to work, the base Application model has to inherit from modelcluster.models.ClusterableModel
(the Wagtail Page
model does this as standard) and the child model has to be linked by a ParentalKey
rather than a ForeignKey
.
子模型通常也是 Orderable
的子类,因此可以对其进行排序.我不记得这是绝对要求还是仅仅是建议.
Child models in an InlinePanel
are also typically subclasses of Orderable
, so they can be sorted; off the top of my head I can't remember whether this is an absolute requirement, or just a recommendation.
from modelcluster.models import ClusterableModel
class Application(ClusterableModel):
# ...
class Reference(Orderable):
application = models.ParentalKey(
Application,
related_name='references',
on_delete=models.CASCADE,
blank=False,
)
这篇关于如何在ModelAdmin中使用InlinePanel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!