From 23fa913676d66ce4d32501618b1b74e8b2dca5fc Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Fri, 8 May 2009 12:53:14 +0000 Subject: [PATCH] Fixed #10448: correcting errors on "save as new" now correctly create a new object instead of modifying the old one. Thanks, bastih. git-svn-id: http://code.djangoproject.com/svn/django/trunk@10713 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/options.py | 4 +-- tests/regressiontests/admin_views/models.py | 1 + tests/regressiontests/admin_views/tests.py | 28 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index e73ea8f388..8297eca74e 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -780,7 +780,7 @@ class ModelAdmin(BaseModelAdmin): 'app_label': opts.app_label, } context.update(extra_context or {}) - return self.render_change_form(request, context, add=True) + return self.render_change_form(request, context, form_url=form_url, add=True) add_view = transaction.commit_on_success(add_view) def change_view(self, request, object_id, extra_context=None): @@ -803,7 +803,7 @@ class ModelAdmin(BaseModelAdmin): raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_unicode(opts.verbose_name), 'key': escape(object_id)}) if request.method == 'POST' and request.POST.has_key("_saveasnew"): - return self.add_view(request, form_url='../../add/') + return self.add_view(request, form_url='../add/') ModelForm = self.get_form(request, obj) formsets = [] diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py index 1aeaea1b1d..f09190b013 100644 --- a/tests/regressiontests/admin_views/models.py +++ b/tests/regressiontests/admin_views/models.py @@ -174,6 +174,7 @@ class PersonAdmin(admin.ModelAdmin): list_filter = ('gender',) search_fields = (u'name',) ordering = ["id"] + save_as = True class Persona(models.Model): """ diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py index 774f79f1b1..8844713dba 100644 --- a/tests/regressiontests/admin_views/tests.py +++ b/tests/regressiontests/admin_views/tests.py @@ -234,6 +234,34 @@ class AdminViewBasicTest(TestCase): "Changelist filter isn't showing options contained inside a model field 'choices' option named group." ) +class SaveAsTests(TestCase): + fixtures = ['admin-views-users.xml','admin-views-person.xml'] + + def setUp(self): + self.client.login(username='super', password='secret') + + def tearDown(self): + self.client.logout() + + def test_save_as_duplication(self): + """Ensure save as actually creates a new person""" + post_data = {'_saveasnew':'', 'name':'John M', 'gender':1} + response = self.client.post('/test_admin/admin/admin_views/person/1/', post_data) + self.assertEqual(len(Person.objects.filter(name='John M')), 1) + self.assertEqual(len(Person.objects.filter(id=1)), 1) + + def test_save_as_display(self): + """ + Ensure that 'save as' is displayed when activated and after submitting + invalid data aside save_as_new will not show us a form to overwrite the + initial model. + """ + response = self.client.get('/test_admin/admin/admin_views/person/1/') + self.assert_(response.context['save_as']) + post_data = {'_saveasnew':'', 'name':'John M', 'gender':3, 'alive':'checked'} + response = self.client.post('/test_admin/admin/admin_views/person/1/', post_data) + self.assertEqual(response.context['form_url'], '../add/') + class CustomModelAdminTest(AdminViewBasicTest): urlbit = "admin2"