在 Django Admin 中从另一个模型的上下文中管理一个模型
这甚至可能是不可能的,但有件事告诉我,我还没有找到一种方法来做到这一点。作为一个简化的示例,我将使用博客的概念。
在更改列表视图中,有多个博客。我选择一个博客进行编辑,然后从更改视图中,我看到属于该博客的帖子的更改列表。当我从那里添加/编辑帖子时,它通过 URL 或传递上下文的其他方式知道它属于哪个博客。
我知道如何设置管理模板来实现我想要的,但我一生都无法弄清楚如何将上下文对象的 id 传递到子对象的视图。有什么建议吗?
编辑:
对不起大家。根据我得到的答案,我一定不够具体。
内联不是我正在寻找的。每个子对象都包含大量数据。使用内联会导致页面永远滚动,无法轻松访问特定对象。我需要有一个更改列表样式的外观(内联所在的位置),单击那里的链接将带您进入该对象的正常更改视图,同时以某种方式将上下文对象的 id 传递给视图。
例如。如果我访问特定博客,则 URL 将类似于:
/admin/blog/blog/1/
然后,从那里单击帖子,我发送到的 URL 类似于:
/admin/blog/blog/1/post/1/
博客 ID(本例中为 1
)将可供帖子对象使用,以便我可以保存其博客外键。这也不一定必须通过 URL 来完成。我只需要某种方法为正在编辑的帖子提供上下文,该帖子位于具有该 ID 的博客上下文中。
This may not even be possible, but something tells me I just haven't figure out a way to do it yet. As a simplified example, I'll use the concept of a blog.
In the changelist view, there's multiple blogs. I select one blog to edit, and from the change view, I see a changelist of posts that belong to that blog. When I add/edit a post from there, it knows which blog it belongs to either through the URL or some other means of passing the context.
I know how to set up the admin templates to achieve what I want, but I can't for the life of me figure out how to pass the id of the context object to the child object's view. Any suggestions?
EDIT:
Sorry guys. Based on the answers I'm getting I must have not been specific enough.
Inlines are not what I'm looking for. Each of the child objects contain a lot of data. Using inlines results in a page that scrolls on forever with no way to easily access a specific object. I need to have a changelist-style look (where the inlines would have been), and clicking on a link there would take you the normal change view for the object, while somehow passing the id of the context object to the view.
For example. If I went to a specific blog the URL would be something like:
/admin/blog/blog/1/
Then, from there I click on a post and the URL I'm sent to is something like:
/admin/blog/blog/1/post/1/
The blog id (1
in this example) would be available to the post object, so that I could save its blog foreign key. This doesn't have to be done by URLs, necessarily, either. I just need some method of providing context to the post being edited that its within the context of the blog with that id.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
传递parent_id 的问题仅与“创建新”页面相关,对吗?对于任何现有模型,我们都已经知道这个 id。
让我们将问题分为两部分:
1.将parent_id传递到新页面
2.读取新页面上的parent_id并相应地修改表单
良好的稳健解决方案将涉及修改父模型和子模型的管理视图。考虑到查看源代码时这些视图有多么复杂,工作量太大了。因此,我个人会选择快速而肮脏的 JS hack:
将parent_id 作为获取参数添加到更改列表中的链接。
在子页面,如果GET参数中有parent_id,则使用JS设置字段值。这仍然允许用户稍后更改它。如果您不想更改字段值,请禁用该字段并创建新的隐藏字段,其名称和值与parent_id相同。
Problem of passing parent_id is only relevant to "create new" page, right? For any existing model we already know this id.
Let's split the problem in 2 parts:
1. Pass parent_id to new page
2. Read parent_id on new page and modify form accordingly
Good robust solution would involve modifying admin views for both parent and child models. Too much work, considering how complex those views are when you look at source. So, I'd personally settle with quick and dirty JS hack:
Add parent_id as get parameter to links in changelists.
On child page, if there's parent_id in GET params, use JS to set field value. This will still allow user to change it later. If you don't want field value changeable, disable the field and create new hidden field with same name and parent_id as value.
您想从管理员内部链接到更改视图吗?这应该有帮助:
So you want to link to a change view from within the admin? This should help:
查看管理内联: http://docs.djangoproject.com /en/1.3/ref/contrib/admin/#inlinemodeladmin-objects
它们允许您将子对象添加到父对象的详细信息页面。
Look at admin inlines: http://docs.djangoproject.com/en/1.3/ref/contrib/admin/#inlinemodeladmin-objects
They allow you to add child objects to parent object's details page.