Django admin - 内联内联(或者,一次编辑三个模型)

发布于 2024-07-15 12:54:42 字数 1195 浏览 15 评论 0原文

我有一组如下所示的模型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

平生欢 2024-07-22 12:54:42

您需要创建一个自定义 表单LinkSectionInline 的“http://docs.djangoproject.com/en/dev/ref/contrib/admin/#template” rel="noreferrer">模板

像这样的东西应该适用于表单:(

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

这只是我的想法,没有经过测试,但它应该让你朝着正确的方向前进。)

你的模板只需要适当地渲染表单和 form.link_formset 。

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.

π浅易 2024-07-22 12:54:42

Django-nested-inlines 就是为此而构建的。 使用方法很简单。

from django.contrib import admin
from nested_inlines.admin import NestedModelAdmin, NestedStackedInline, NestedTabularInline
from models import A, B, C

class MyNestedInline(NestedTabularInline):
    model = C

class MyInline(NestedStackedInline):
    model = B
    inlines = [MyNestedInline,]

class MyAdmin(NestedModelAdmin):
    pass

admin.site.register(A, MyAdmin)

Django-nested-inlines is built for just this. Usage is simple.

from django.contrib import admin
from nested_inlines.admin import NestedModelAdmin, NestedStackedInline, NestedTabularInline
from models import A, B, C

class MyNestedInline(NestedTabularInline):
    model = C

class MyInline(NestedStackedInline):
    model = B
    inlines = [MyNestedInline,]

class MyAdmin(NestedModelAdmin):
    pass

admin.site.register(A, MyAdmin)
总以为 2024-07-22 12:54:42

我的建议实际上是改变你的模型。 为什么不在 LinkLinkSection 中使用 ForeignKey? 或者,如果不是 OneToMany,也许是 ManyToMany 字段? 管理界面将免费生成它。 当然,如果链接在逻辑上与链接部分没有任何关系,我不建议这样做,但也许它们有关系? 如果不这样做,请解释预期的组织是什么。 (例如,每个部分 3 个链接是固定的还是任意的?)

My recommendation would actually be to change your model. Why not have a ForeignKey in Link to LinkSection? Or, if it's not OneToMany, perhaps a ManyToMany 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?)

你丑哭了我 2024-07-22 12:54:42

您可以创建一个类似于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文