Django admin - inline inlines (or, three model editing at once)(Django admin - 内联内联(或一次编辑三个模型))
问题描述
我有一组看起来像这样的模型:
类页面(models.Model):标题 = models.CharField(max_length=255)类 LinkSection(models.Model):page = models.ForeignKey(Page)标题 = models.CharField(max_length=255)类链接(模型.模型):linksection = models.ForeignKey(LinkSection)文本 = models.CharField(max_length=255)url = 模型.URLField()
还有一个如下所示的 admin.py:
类 LinkInline(admin.TabularInline):型号=链接类 LinkSectionInline(admin.TabularInline):模型 = 链接部分内联= [链接内联,]类 PageAdmin(admin.ModelAdmin):内联 = [ LinkSectionInline, ]
我的目标是获得一个管理界面,让我可以在一页上编辑所有内容.这种模型结构的最终结果是,事物被生成为一个看起来或多或少像的视图+模板:
<h1>{{page.title}}</h1>{% for ls in page.linksection_set.objects.all %}<h2>{{ls.title}}</h2><ul>{% for l in ls.link_set.objects.all %}<li><a href="{{l.url}}">{{l.title}}</a></li>{% endfor %}</ul></div>{% endfor %}正如我所料,我知道 Django 管理员中的 inline-in-an-inline 技巧失败了.有谁知道允许这种三级模型编辑的方法?提前致谢.
解决方案 你需要创建一个自定义 表单 和 模板 用于 LinkSectionInline
.
这样的东西应该适用于表单:
LinkFormset = forms.modelformset_factory(Link)类 LinkSectionForm(forms.ModelForm):def __init__(self, **kwargs):super(LinkSectionForm, self).__init__(**kwargs)self.link_formset = LinkFormset(instance=self.instance,数据=self.data 或无,前缀=self.prefix)def is_valid(self):return (super(LinkSectionForm, self).is_valid() 和self.link_formset.is_valid())def保存(自我,提交=真):# 支持 commit=False 是另一种蠕虫.没有用的交易# 在需要它之前.(雅尼)断言提交 == 真res = super(LinkSectionForm, self).save(commit=commit)self.link_formset.save()返回资源
(这只是我的想法,没有经过测试,但它应该能让你朝着正确的方向前进.)
您的模板只需要适当地呈现表单和 form.link_formset.
I've got a set of models that look like this:
class Page(models.Model):
title = models.CharField(max_length=255)
class LinkSection(models.Model):
page = models.ForeignKey(Page)
title = models.CharField(max_length=255)
class Link(models.Model):
linksection = models.ForeignKey(LinkSection)
text = models.CharField(max_length=255)
url = models.URLField()
and an admin.py that looks like this:
class LinkInline(admin.TabularInline):
model = Link
class LinkSectionInline(admin.TabularInline):
model = LinkSection
inlines = [ LinkInline, ]
class PageAdmin(admin.ModelAdmin):
inlines = [ LinkSectionInline, ]
My goal is to get an admin interface that lets me edit everything on one page. The end result of this model structure is that things are generated into a view+template that looks more or less like:
<h1>{{page.title}}</h1>
{% for ls in page.linksection_set.objects.all %}
<div>
<h2>{{ls.title}}</h2>
<ul>
{% for l in ls.link_set.objects.all %}
<li><a href="{{l.url}}">{{l.title}}</a></li>
{% endfor %}
</ul>
</div>
{% endfor %}
I know that the inline-in-an-inline trick fails in the Django admin, as I expected. Does anyone know of a way to allow this kind of three level model editing? Thanks in advance.
解决方案 You need to create a custom form and template for the LinkSectionInline
.
Something like this should work for the form:
LinkFormset = forms.modelformset_factory(Link)
class LinkSectionForm(forms.ModelForm):
def __init__(self, **kwargs):
super(LinkSectionForm, self).__init__(**kwargs)
self.link_formset = LinkFormset(instance=self.instance,
data=self.data or None,
prefix=self.prefix)
def is_valid(self):
return (super(LinkSectionForm, self).is_valid() and
self.link_formset.is_valid())
def save(self, commit=True):
# Supporting commit=False is another can of worms. No use dealing
# it before it's needed. (YAGNI)
assert commit == True
res = super(LinkSectionForm, self).save(commit=commit)
self.link_formset.save()
return res
(That just came off the top of my head and isn't tested, but it should get you going in the right direction.)
Your template just needs to render the form and form.link_formset appropriately.
这篇关于Django admin - 内联内联(或一次编辑三个模型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Django admin - 内联内联(或一次编辑三个模型)
基础教程推荐
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01