From d68666700fa3ca24701479bd0831c3dcfb9f5ab1 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Fri, 12 Aug 2011 14:14:24 +0000 Subject: [PATCH] Fixed #16604 -- Use new save_related hook when saving objects on the changelist, too. Thanks, pdeglopper. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16599 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/options.py | 2 +- tests/regressiontests/admin_views/models.py | 2 ++ tests/regressiontests/admin_views/tests.py | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 81e8ae5614..922205f8cd 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -1146,7 +1146,7 @@ class ModelAdmin(BaseModelAdmin): if form.has_changed(): obj = self.save_form(request, form, change=True) self.save_model(request, obj, form, change=True) - form.save_m2m() + self.save_related(request, form, formsets=[], change=True) change_msg = self.construct_change_message(request, form, None) self.log_change(request, obj, change_msg) changecount += 1 diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py index ce669a4005..8dc61e34e5 100644 --- a/tests/regressiontests/admin_views/models.py +++ b/tests/regressiontests/admin_views/models.py @@ -391,6 +391,8 @@ class ParentAdmin(admin.ModelAdmin): model = Parent inlines = [ChildInline] + list_editable = ('name',) + def save_related(self, request, form, formsets, change): super(ParentAdmin, self).save_related(request, form, formsets, change) first_name, last_name = form.instance.name.split() diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index 7128f04a64..8daa4662a9 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -3160,3 +3160,22 @@ class AdminCustomSaveRelatedTests(TestCase): self.assertEqual('Josh Stone', Parent.objects.latest('id').name) self.assertEqual([u'Catherine Stone', u'Paul Stone'], children_names) + + def test_should_be_able_to_edit_related_objects_on_changelist_view(self): + parent = Parent.objects.create(name='Josh Rock') + paul = Child.objects.create(parent=parent, name='Paul') + catherine = Child.objects.create(parent=parent, name='Catherine') + post = { + 'form-TOTAL_FORMS': '1', + 'form-INITIAL_FORMS': '1', + 'form-MAX_NUM_FORMS': '0', + 'form-0-id': parent.id, + 'form-0-name': 'Josh Stone', + '_save': 'Save' + } + + response = self.client.post('/test_admin/admin/admin_views/parent/', post) + children_names = list(Child.objects.order_by('name').values_list('name', flat=True)) + + self.assertEqual('Josh Stone', Parent.objects.latest('id').name) + self.assertEqual([u'Catherine Stone', u'Paul Stone'], children_names)