From 61524b09cfa3b51643d0e79cbf0e1e08ede357ae Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Sat, 1 Jun 2013 18:16:57 -0400 Subject: [PATCH] Fixed #18388 - Added InlineModelAdmin.get_max_num hook. Thanks d.willy.c.c@ for the suggestion and Melevir and Areski Belaid for work on the patch. --- django/contrib/admin/options.py | 6 +++++- docs/ref/contrib/admin/index.txt | 26 ++++++++++++++++++++++++++ docs/releases/1.6.txt | 5 +++-- tests/admin_inlines/admin.py | 9 +++++++-- tests/admin_inlines/tests.py | 6 ++++++ 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 97f82cbb592..34583ebf744 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -1516,6 +1516,10 @@ class InlineModelAdmin(BaseModelAdmin): """Hook for customizing the number of extra inline forms.""" return self.extra + def get_max_num(self, request, obj=None, **kwargs): + """Hook for customizing the max number of extra inline forms.""" + return self.max_num + def get_formset(self, request, obj=None, **kwargs): """Returns a BaseInlineFormSet class for use in admin add/change views.""" if 'fields' in kwargs: @@ -1543,7 +1547,7 @@ class InlineModelAdmin(BaseModelAdmin): "exclude": exclude, "formfield_callback": partial(self.formfield_for_dbfield, request=request), "extra": self.get_extra(request, obj, **kwargs), - "max_num": self.max_num, + "max_num": self.get_max_num(request, obj, **kwargs), "can_delete": can_delete, } diff --git a/docs/ref/contrib/admin/index.txt b/docs/ref/contrib/admin/index.txt index 73ea74adc07..8f457e77ac3 100644 --- a/docs/ref/contrib/admin/index.txt +++ b/docs/ref/contrib/admin/index.txt @@ -1728,6 +1728,11 @@ The ``InlineModelAdmin`` class adds: doesn't directly correlate to the number of objects, but can if the value is small enough. See :ref:`model-formsets-max-num` for more information. + .. versionadded:: 1.6 + + :meth:`InlineModelAdmin.get_max_num` also allows you to customize the + maximum number of extra forms. + .. attribute:: InlineModelAdmin.raw_id_fields By default, Django's admin uses a select-box interface (' # The total number of forms will remain the same in either case total_forms_hidden = '' + response = self.client.get('/admin/admin_inlines/binarytree/add/') + self.assertContains(response, max_forms_input % 3) self.assertContains(response, total_forms_hidden) response = self.client.get("/admin/admin_inlines/binarytree/%d/" % bt_head.id) + self.assertContains(response, max_forms_input % 2) self.assertContains(response, total_forms_hidden)