Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
  • Loading branch information...
commit 23fa913676d66ce4d32501618b1b74e8b2dca5fc 1 parent 38a6c48
@jacobian jacobian authored
View
4 django/contrib/admin/options.py
@@ -780,7 +780,7 @@ def add_view(self, request, form_url='', extra_context=None):
'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 @@ def change_view(self, request, object_id, extra_context=None):
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 = []
View
1  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):
"""
View
28 tests/regressiontests/admin_views/tests.py
@@ -234,6 +234,34 @@ def testNamedGroupFieldChoicesFilter(self):
"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"
Please sign in to comment.
Something went wrong with that request. Please try again.