From cc74d8e02e8b659b914859d60002aaa47600f0ff Mon Sep 17 00:00:00 2001 From: Justin Caratzas Date: Mon, 22 Sep 2014 17:29:44 -0400 Subject: [PATCH] [1.7.x] Fixed #23539 -- Added get_extra(), get_max_num(), and get_min_num() hooks to GenericInlineModelAdmin. Backport of cf43a1ee90 from master --- django/contrib/contenttypes/admin.py | 6 +-- docs/releases/1.7.1.txt | 6 +++ tests/generic_inline_admin/tests.py | 57 ++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/django/contrib/contenttypes/admin.py b/django/contrib/contenttypes/admin.py index 1938fa7aae8..3ad4c86ee8f 100644 --- a/django/contrib/contenttypes/admin.py +++ b/django/contrib/contenttypes/admin.py @@ -115,12 +115,12 @@ class GenericInlineModelAdmin(InlineModelAdmin): "form": self.form, "formfield_callback": partial(self.formfield_for_dbfield, request=request), "formset": self.formset, - "extra": self.extra, + "extra": self.get_extra(request, obj), "can_delete": can_delete, "can_order": False, "fields": fields, - "min_num": self.min_num, - "max_num": self.max_num, + "min_num": self.get_min_num(request, obj), + "max_num": self.get_max_num(request, obj), "exclude": exclude } defaults.update(kwargs) diff --git a/docs/releases/1.7.1.txt b/docs/releases/1.7.1.txt index aa7d7f90a08..8d8af5c7f29 100644 --- a/docs/releases/1.7.1.txt +++ b/docs/releases/1.7.1.txt @@ -61,3 +61,9 @@ Bugfixes * Fixed renaming of models with a self-referential many-to-many field (``ManyToManyField('self')``) (:ticket:`23503`). + +* Added the :meth:`~django.contrib.admin.InlineModelAdmin.get_extra()`, + :meth:`~django.contrib.admin.InlineModelAdmin.get_max_num()`, and + :meth:`~django.contrib.admin.InlineModelAdmin.get_min_num()` hooks to + :class:`~django.contrib.contenttypes.admin.GenericInlineModelAdmin` + (:ticket:`23539`). diff --git a/tests/generic_inline_admin/tests.py b/tests/generic_inline_admin/tests.py index 631671e803b..6a9c783bf00 100644 --- a/tests/generic_inline_admin/tests.py +++ b/tests/generic_inline_admin/tests.py @@ -221,6 +221,63 @@ class GenericInlineAdminParametersTest(TestCase): self.assertEqual(formset.total_form_count(), 5) self.assertEqual(formset.initial_form_count(), 1) + def test_get_extra(self): + + class GetExtraInline(GenericTabularInline): + model = Media + extra = 4 + + def get_extra(self, request, obj): + return 2 + + modeladmin = admin.ModelAdmin(Episode, admin_site) + modeladmin.inlines = [GetExtraInline] + e = self._create_object(Episode) + request = self.factory.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk) + request.user = User(username='super', is_superuser=True) + response = modeladmin.changeform_view(request, object_id=str(e.pk)) + formset = response.context_data['inline_admin_formsets'][0].formset + + self.assertEqual(formset.extra, 2) + + def test_get_min_num(self): + + class GetMinNumInline(GenericTabularInline): + model = Media + min_num = 5 + + def get_min_num(self, request, obj): + return 2 + + modeladmin = admin.ModelAdmin(Episode, admin_site) + modeladmin.inlines = [GetMinNumInline] + e = self._create_object(Episode) + request = self.factory.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk) + request.user = User(username='super', is_superuser=True) + response = modeladmin.changeform_view(request, object_id=str(e.pk)) + formset = response.context_data['inline_admin_formsets'][0].formset + + self.assertEqual(formset.min_num, 2) + + def test_get_max_num(self): + + class GetMaxNumInline(GenericTabularInline): + model = Media + extra = 5 + + def get_max_num(self, request, obj): + return 2 + + modeladmin = admin.ModelAdmin(Episode, admin_site) + modeladmin.inlines = [GetMaxNumInline] + e = self._create_object(Episode) + request = self.factory.get('/generic_inline_admin/admin/generic_inline_admin/episode/%s/' % e.pk) + request.user = User(username='super', is_superuser=True) + response = modeladmin.changeform_view(request, object_id=str(e.pk)) + formset = response.context_data['inline_admin_formsets'][0].formset + + self.assertEqual(formset.max_num, 2) + @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',)) class GenericInlineAdminWithUniqueTogetherTest(TestCase):