Fixed #17856 -- Passed obj to get_inline_instances

Thanks ybon, quinode and sjaensch for the patch, and Tim Graham
for the review.
This commit is contained in:
Aymeric Augustin 2012-10-20 15:48:38 +02:00
parent dcbf08cce5
commit c2e19e26bc
2 changed files with 16 additions and 6 deletions

View File

@ -346,14 +346,14 @@ class ModelAdmin(BaseModelAdmin):
self.admin_site = admin_site self.admin_site = admin_site
super(ModelAdmin, self).__init__() super(ModelAdmin, self).__init__()
def get_inline_instances(self, request): def get_inline_instances(self, request, obj=None):
inline_instances = [] inline_instances = []
for inline_class in self.inlines: for inline_class in self.inlines:
inline = inline_class(self.model, self.admin_site) inline = inline_class(self.model, self.admin_site)
if request: if request:
if not (inline.has_add_permission(request) or if not (inline.has_add_permission(request) or
inline.has_change_permission(request) or inline.has_change_permission(request, obj) or
inline.has_delete_permission(request)): inline.has_delete_permission(request, obj)):
continue continue
if not inline.has_add_permission(request): if not inline.has_add_permission(request):
inline.max_num = 0 inline.max_num = 0
@ -506,7 +506,7 @@ class ModelAdmin(BaseModelAdmin):
fields=self.list_editable, **defaults) fields=self.list_editable, **defaults)
def get_formsets(self, request, obj=None): def get_formsets(self, request, obj=None):
for inline in self.get_inline_instances(request): for inline in self.get_inline_instances(request, obj):
yield inline.get_formset(request, obj) yield inline.get_formset(request, obj)
def get_paginator(self, request, queryset, per_page, orphans=0, allow_empty_first_page=True): def get_paginator(self, request, queryset, per_page, orphans=0, allow_empty_first_page=True):
@ -994,7 +994,7 @@ class ModelAdmin(BaseModelAdmin):
ModelForm = self.get_form(request) ModelForm = self.get_form(request)
formsets = [] formsets = []
inline_instances = self.get_inline_instances(request) inline_instances = self.get_inline_instances(request, None)
if request.method == 'POST': if request.method == 'POST':
form = ModelForm(request.POST, request.FILES) form = ModelForm(request.POST, request.FILES)
if form.is_valid(): if form.is_valid():
@ -1091,7 +1091,7 @@ class ModelAdmin(BaseModelAdmin):
ModelForm = self.get_form(request, obj) ModelForm = self.get_form(request, obj)
formsets = [] formsets = []
inline_instances = self.get_inline_instances(request) inline_instances = self.get_inline_instances(request, obj)
if request.method == 'POST': if request.method == 'POST':
form = ModelForm(request.POST, request.FILES, instance=obj) form = ModelForm(request.POST, request.FILES, instance=obj)
if form.is_valid(): if form.is_valid():

View File

@ -1046,6 +1046,16 @@ templates used by the :class:`ModelAdmin` views:
changelist that will be linked to the change view, as described in the changelist that will be linked to the change view, as described in the
:attr:`ModelAdmin.list_display_links` section. :attr:`ModelAdmin.list_display_links` section.
.. method:: ModelAdmin.get_inline_instances(self, request, obj=None)
.. versionadded:: 1.5
The ``get_inline_instances`` method is given the ``HttpRequest`` and the
``obj`` being edited (or ``None`` on an add form) and is expected to return
a ``list`` or ``tuple`` of :class:`~django.contrib.admin.InlineModelAdmin`
objects, as described below in the :class:`~django.contrib.admin.InlineModelAdmin`
section.
.. method:: ModelAdmin.get_urls(self) .. method:: ModelAdmin.get_urls(self)
The ``get_urls`` method on a ``ModelAdmin`` returns the URLs to be used for The ``get_urls`` method on a ``ModelAdmin`` returns the URLs to be used for