Django admin - 内联内联(或者,一次编辑三个模型)
我有一组如下所示的模型:
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()
和一个如下所示的 admin.py:
class LinkInline(admin.TabularInline):
model = Link
class LinkSectionInline(admin.TabularInline):
model = LinkSection
inlines = [ LinkInline, ]
class PageAdmin(admin.ModelAdmin):
inlines = [ LinkSectionInline, ]
我的目标是获得一个管理界面,让我可以编辑一个页面上的所有内容。 这个模型结构的最终结果是,东西被生成到一个视图+模板中,看起来或多或少像:
<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 %}
我知道 inline-in-an-inline 技巧在 Django 管理中失败了,正如我所期望的那样。 有谁知道允许这种三级模型编辑的方法吗? 提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要创建一个自定义 表单 和 LinkSectionInline 的“http://docs.djangoproject.com/en/dev/ref/contrib/admin/#template” rel="noreferrer">模板。
像这样的东西应该适用于表单:(
这只是我的想法,没有经过测试,但它应该让你朝着正确的方向前进。)
你的模板只需要适当地渲染表单和 form.link_formset 。
You need to create a custom form and template for the
LinkSectionInline
.Something like this should work for the form:
(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-nested-inlines 就是为此而构建的。 使用方法很简单。
Django-nested-inlines is built for just this. Usage is simple.
我的建议实际上是改变你的模型。 为什么不在
Link
到LinkSection
中使用ForeignKey
? 或者,如果不是 OneToMany,也许是ManyToMany
字段? 管理界面将免费生成它。 当然,如果链接在逻辑上与链接部分没有任何关系,我不建议这样做,但也许它们有关系? 如果不这样做,请解释预期的组织是什么。 (例如,每个部分 3 个链接是固定的还是任意的?)My recommendation would actually be to change your model. Why not have a
ForeignKey
inLink
toLinkSection
? Or, if it's not OneToMany, perhaps aManyToMany
field? The admin interface will generate that for free. Of course, I don't recommend this if links don't logically have anything to do with link sections, but maybe they do? If they don't, please explain what the intended organization is. (For example, is 3 links per section fixed or arbitrary?)您可以创建一个类似于 TabularInline 或 StackedInline 的新类,它本身能够使用内联字段。
或者,您可以专门为您的模型创建新的管理模板。 但这当然否定了管理界面的漂亮功能。
You can create a new class, similar to TabularInline or StackedInline, that is able to use inline fields itself.
Alternatively, you can create new admin templates, specifically for your model. But that of course overrules the nifty features of the admin interface.