在 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.
相关文章