In Django Admin, how do you have cascaded InlineModelAdmin
Hi I have a list of restaurants (e.g. McDonalds, etc), with menus (e.g Lunch Menu, Dinner Menu), submenus (e.g. Appetizers, Sandwidches, etc.) and dishes (Angus Burger, Chicken Burger, etc.)
They are all linked by foreign keys.
Is there a way, so that in the Restaurant admin, I have Menu as a tabular inline, with a link to go edit that menu (in it's own开发者_开发知识库 admin page, not inline) so that there I can display the submenus inline with the menu admin. Each submenu has a link to edit that submenu item (in it's own admin page, not inline) so that there I can display the dishes inline.
THanks.
If you just want to append some links like in the Restaurant change view, you don't really need to use an inline as this provides a form for changing data. I would probably override change_view in RestaurantAdmin to get a list of the related menus and pass it as an extra_context. And then override the model specific change_form.html template to render the menu links.
So you want to add field to your MenuInline
that links to the change_form
of each menu?
This can be done using read_only
fields.
First i would add a method to your Menu model that creates the link to the model's change_form
in the admin. See Reversing admin URLs for some pointers.
You should end up with something similar to this:
def get_menu_admin_page(self):
from django.core.urlresolvers import reverse
return '<a href="%s">%s</a>' % (reverse('admin:yourapp_menu_change',
args=(self.pk,)), self.menu_title)
get_menu_admin_page.allow_tags = True
Now you can add this method to your InlineAdmin
.
class MenuInline(admin.TabularInline):
model = Menu
fields = ('menu_title', 'get_menu_admin_page')
readonly_fields = ('get_menu_admin_page',)
Now your TabularInline
should contain a link to the change_view
for each individual menu.
精彩评论