Q: 如何在一个model的新增或编辑页面里面内嵌另一个model的编辑页面
A:
- 定义一个Inline类
- 在model的OptionClass里面讲Inline类注入到inlines属性里面
举例:
在IOC这个model里面内嵌了两个一对多的model. IOC和FileAttachment
xadmin.py
from .models import Case, Ioc, FileAttachment
class IocInline:
model = Ioc
extra = 0
class FileAttachmentInline:
model = FileAttachment
extra = 0
class CaseForm(ModelForm):
# 自定义widgets
tags = SimpleArrayField(CharField(), widget=Select2Widget, required=False)
class CaseAdmin:
list_display = ["name", "happen_datetime", "TLP", "tags", "len_ioc", "len_file", "update_user", "update_time"]
search_fields = ["name", "TLP", "tags", "update_user"]
list_filter = ["name", "happen_datetime", "TLP", "update_user", "update_time"]
# 因为自定义了widgets 所以设置了form属性
form = CaseForm
# 在同一编辑界面,内联IOC编辑
inlines = [IocInline, FileAttachmentInline]
list_export = []
效果图:
点击+ 即可新增多个