开发者

Managing one model from within the context of another in Django Admin

开发者 https://www.devze.com 2023-03-08 03:15 出处:网络
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.

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.


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:

  1. Add parent_id as get parameter to links in changelists.

    $("#changelists_container .link_class").each(function(i,link){
      link.attr("href", link.attr("href") + "?parent_id=" parentId; // assumes no other GET params
    });
    
  2. 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:

{% url admin:app_model_change object.pk %}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消