From 30e842632e6ed95828241fb62f50eb1da9e4019c Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Thu, 16 Jun 2011 16:35:04 +0000 Subject: [PATCH] Fixed #9588 -- Allow passing kwargs to `GenericInlineModelAdmin.get_formset` just like the InlineModelAdmin. Thanks, prestontimmons. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16419 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/contenttypes/generic.py | 3 ++- .../generic_inline_admin/tests.py | 20 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/django/contrib/contenttypes/generic.py b/django/contrib/contenttypes/generic.py index b025f5aa95..389692d5b3 100644 --- a/django/contrib/contenttypes/generic.py +++ b/django/contrib/contenttypes/generic.py @@ -396,7 +396,7 @@ class GenericInlineModelAdmin(InlineModelAdmin): ct_fk_field = "object_id" formset = BaseGenericInlineFormSet - def get_formset(self, request, obj=None): + def get_formset(self, request, obj=None, **kwargs): if self.declared_fieldsets: fields = flatten_fieldsets(self.declared_fieldsets) else: @@ -420,6 +420,7 @@ class GenericInlineModelAdmin(InlineModelAdmin): "max_num": self.max_num, "exclude": exclude } + defaults.update(kwargs) return generic_inlineformset_factory(self.model, **defaults) class GenericStackedInline(GenericInlineModelAdmin): diff --git a/tests/regressiontests/generic_inline_admin/tests.py b/tests/regressiontests/generic_inline_admin/tests.py index c40b6dd55b..00badc9228 100644 --- a/tests/regressiontests/generic_inline_admin/tests.py +++ b/tests/regressiontests/generic_inline_admin/tests.py @@ -1,12 +1,13 @@ # coding: utf-8 from django.conf import settings +from django.contrib.admin.sites import AdminSite from django.contrib.contenttypes.generic import generic_inlineformset_factory from django.test import TestCase # local test models -from models import Episode, EpisodeExtra, EpisodeMaxNum, EpisodeExclude, \ - Media, EpisodePermanent, MediaPermanentInline, Category +from models import (Episode, EpisodeExtra, EpisodeMaxNum, EpisodeExclude, + Media, MediaInline, EpisodePermanent, MediaPermanentInline, Category) class GenericAdminViewTest(TestCase): @@ -214,3 +215,18 @@ class NoInlineDeletionTest(TestCase): fake_request = object() formset = inline.get_formset(fake_request) self.assertFalse(formset.can_delete) + +class GenericInlineModelAdminTest(TestCase): + + def test_get_formset_kwargs(self): + media_inline = MediaInline(Media, AdminSite()) + + # Create a formset with default arguments + formset = media_inline.get_formset(None) + self.assertEqual(formset.max_num, None) + self.assertEqual(formset.can_order, False) + + # Create a formset with custom keyword arguments + formset = media_inline.get_formset(None, max_num=100, can_order=True) + self.assertEqual(formset.max_num, 100) + self.assertEqual(formset.can_order, True)