Python Django 管理员中的嵌套内联?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3681258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nested inlines in the Django admin?
提问by Cody Hatch
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 wantis 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.
我想要的是让用户能够进入管理界面,添加一个新的更新,然后在编辑更新时添加一个或多个帖子,每个帖子都有一个或多个媒体项目。此外,我希望用户能够在更新中重新排序帖子。
My current attempt has the following in admin.py:
我目前的尝试在 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.
这让用户添加一个新的 Post 项目,选择相关的更新,向其中添加媒体项目,然后点击保存 - 这很好。但是没有办法在一个地方看到属于给定更新的所有帖子,这反过来意味着你不能在更新中编辑帖子。对于最终用户来说,这真的很令人困惑。
Help?
帮助?
采纳答案by Vasil
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/9025for 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.contrib.admin 中还没有“内置”方式来嵌套内联(内联内联)。通过拥有自己的 ModelAdmin 和 InlineModelAdmin 子类来启用这种功能,可以实现类似的功能。有关如何实现这一点的想法,请参阅此票证http://code.djangoproject.com/ticket/9025上的补丁。您还需要提供自己的模板,这些模板将在顶级内联及其子内联上进行嵌套迭代。
回答by carruthd
I ran into a similar issue to this. My approach was to make an UpdateAdmin that held inlines for both Media and Post... it basically just makes it so you have a list of all of the media entries followed by all of the posts in an update.
我遇到了与此类似的问题。我的方法是创建一个 UpdateAdmin,它同时为 Media 和 Post 保存内联......它基本上只是让它这样你有一个所有媒体条目的列表,然后是更新中的所有帖子。
class MediaInline(admin.StackedInline):
model = Media
class PostInline(admin.StackedInline):
model = Post
class PostAdmin(admin.ModelAdmin):
inlines = [MediaInline,]
class UpdateAdmin(admin.ModelAdmin):
inlines = [MediaInline,PostInline]
It isn't an ideal solution but it works for a quick and dirty work around.
这不是一个理想的解决方案,但它适用于快速而肮脏的工作。
回答by GreenAsJade
There is now this egg available, which is a collation of the relevant patches mentioned in the other answer:
现在有这个鸡蛋可用,它是另一个答案中提到的相关补丁的整理:
回答by Aivoric
I have just ran into this issue as well... Seems this thread which contains the request for the nested inlines feature (https://code.djangoproject.com/ticket/9025#no2) has been updated with further information.
我也刚刚遇到了这个问题......似乎这个包含对嵌套内联功能(https://code.djangoproject.com/ticket/9025#no2)的请求的线程已经更新了更多信息。
A custom made app called "django-super-inline" has been released. More details here: https://github.com/BertrandBordage/django-super-inlines
一个名为“django-super-inline”的定制应用程序已经发布。更多细节在这里:https: //github.com/BertrandBordage/django-super-inlines
Installation and usage instructions below.
安装和使用说明如下。
Hope this is useful for whomever comes across this.
希望这对遇到此问题的人有用。
回答by Li-Wen Yip
I have done this using https://github.com/theatlantic/django-nested-admin, for the following Data structure:
我已经使用https://github.com/theatlantic/django-nested-admin完成了以下数据结构:
- Contest
- Judges
- Contestants
- Singers
- Songs
- 比赛
- 评委
- 参赛者
- 歌手
- 歌曲
My admin.pyfile:
我的admin.py文件:
from django.contrib import admin
import nested_admin
from .models import Contest, Contestant, Judge, Song, Singer
class SongInline(nested_admin.NestedTabularInline):
model = Song
extra = 0
class SingerInline(nested_admin.NestedTabularInline):
model = Singer
extra = 0
class ContestantInline(nested_admin.NestedTabularInline):
model = Contestant
inlines = [SongInline, SingerInline]
extra = 0
class JudgeInline(nested_admin.NestedTabularInline):
model = Judge
extra = 0
class ContestAdmin(nested_admin.NestedModelAdmin):
model = Contest
inlines = [ContestantInline, JudgeInline]
extra = 0
admin.site.register(Contest, ContestAdmin)
https://github.com/theatlantic/django-nested-adminappears to be much more actively maintained than the other apps already mentioned (https://github.com/BertrandBordage/django-super-inlinesand https://github.com/Soaa-/django-nested-inlines)
https://github.com/theatlantic/django-nested-admin似乎比已经提到的其他应用程序维护得更加积极(https://github.com/BertrandBordage/django-super-inlines和https://github .com/Soaa-/django-nested-inlines)


