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

查看:766
本文介绍了在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,]

这是让用户添加一个新的Post项目,选择相关的更新,添加媒体项目到它,并点击保存 - 这是罚款。但是,没有办法在一个地方看到属于给定更新的所有帖子,这反过来又意味着您无法在更新中发布帖子。对于最终用户来说真的很混乱。

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 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天全站免登陆