Django 管理中的嵌套内联? [英] Nested inlines in the Django admin?

查看:37
本文介绍了Django 管理中的嵌套内联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有一个相当简单的设计.

Alright, I have a fairly simple design.

class Update(models.Model):
    pub_date = models.DateField()
    title = models.CharField(max_length=512)

class Post(models.Model):
    update = models.ForeignKey(Update)
    body = models.TextField()
    order = models.PositiveIntegerField(blank=True)

class Media(models.Model):
    post = models.ForeignKey(Post)
    thumb = models.ImageField(upload_to='frontpage')
    fullImagePath = models.ImageField(upload_to='frontpage')

是否有一种简单的方法可以让用户在一个页面上创建所有更新?

Is there an easy-ish way to allow a user to create an update all on one page?

想要是让用户能够进入管理界面,添加新的更新,然后在编辑更新时添加一个或多个帖子,每个帖子有一个或多个更多媒体项目.此外,我希望用户能够在更新中重新排序帖子.

What I want is for a user to be able to go to the admin interface, add a new Update, and then while editing an Update add one or more Posts, with each Post having one or more Media items. In addition, I want the user to be able to reorder Posts within an update.

我当前的尝试在 admin.py 中有以下内容:

My current attempt has the following in admin.py:

class MediaInline(admin.StackedInline):
    model = Media

class PostAdmin(admin.ModelAdmin):
    inlines = [MediaInline,]

这让用户添加一个新的帖子项目,选择相关的更新,向其中添加媒体项目,然后点击保存 - 这很好.但是没有办法在一个地方看到属于给定更新的所有帖子,这反过来意味着你不能在更新中编辑帖子.对于最终用户来说,这真的很令人困惑.

This let's the user add a new Post item, select the relevant Update, add the Media items to it, and hit save - which is fine. But there's no way to see all the Posts that belong to a given Update in a single place, which in turn means you can't roderder Posts within an update. It's really quite confusing for the end user.

帮助?

推荐答案

截至目前,在 django.contrib.admin 中还没有内置"方式来嵌套内联(inline inside inline).通过拥有自己的 ModelAdmin 和 InlineModelAdmin 子类来启用这种功能,可以实现类似的功能.请参阅此票证上的补丁 http://code.djangoproject.com/ticket/9025 以获取想法关于如何实现这一点.您还需要提供自己的模板,这些模板将在顶级内联及其子内联上进行嵌套迭代.

As of now there is no "built-in" way to have nested inlines (inline inside inline) in django.contrib.admin. Pulling something like this off is possible by having your own ModelAdmin and InlineModelAdmin subclasses that would enable this kind of functionality. See the patches on this ticket http://code.djangoproject.com/ticket/9025 for ideas on how to implement this. You'd also need to provide your own templates that would have nested iteration over both the top level inline and it's child inline.

这篇关于Django 管理中的嵌套内联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆