Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.0.X] Fixed #10448: correcting errors on "save as new" now correctl…

…y create a new object instead of modifying the old one. Thanks, bastih. Backport of [10713] from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10764 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 11d08bca2bf99de7e7aefec7986a206ea5039724 1 parent 6803381
@jacobian jacobian authored
View
4 django/contrib/admin/options.py
@@ -549,7 +549,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):
@@ -572,7 +572,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
18 tests/regressiontests/admin_views/fixtures/admin-views-person.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<django-objects version="1.0">
+ <object pk="1" model="admin_views.person">
+ <field type="CharField" name="name">John Mauchly</field>
+ <field type="IntegerField" name="gender">1</field>
+ <field type="BooleanField" name="alive">True</field>
+ </object>
+ <object pk="2" model="admin_views.person">
+ <field type="CharField" name="name">Grace Hooper</field>
+ <field type="IntegerField" name="gender">1</field>
+ <field type="BooleanField" name="alive">False</field>
+ </object>
+ <object pk="3" model="admin_views.person">
+ <field type="CharField" name="name">Guido van Rossum</field>
+ <field type="IntegerField" name="gender">1</field>
+ <field type="BooleanField" name="alive">True</field>
+ </object>
+</django-objects>
View
24 tests/regressiontests/admin_views/models.py
@@ -152,6 +152,29 @@ class FabricAdmin(admin.ModelAdmin):
list_display = ('surface',)
list_filter = ('surface',)
+class Person(models.Model):
+ GENDER_CHOICES = (
+ (1, "Male"),
+ (2, "Female"),
+ )
+ name = models.CharField(max_length=100)
+ gender = models.IntegerField(choices=GENDER_CHOICES)
+ alive = models.BooleanField()
+
+ def __unicode__(self):
+ return self.name
+
+ class Meta:
+ ordering = ["id"]
+
+class PersonAdmin(admin.ModelAdmin):
+ list_display = ('name', 'gender', 'alive')
+ list_editable = ('gender', 'alive')
+ list_filter = ('gender',)
+ search_fields = (u'name',)
+ ordering = ["id"]
+ save_as = True
+
class Persona(models.Model):
"""
A simple persona associated with accounts, to test inlining of related
@@ -308,6 +331,7 @@ class CollectorAdmin(admin.ModelAdmin):
admin.site.register(ModelWithStringPrimaryKey)
admin.site.register(Color)
admin.site.register(Thing, ThingAdmin)
+admin.site.register(Person, PersonAdmin)
admin.site.register(Persona, PersonaAdmin)
admin.site.register(Parent, ParentAdmin)
admin.site.register(EmptyModel, EmptyModelAdmin)
View
30 tests/regressiontests/admin_views/tests.py
@@ -14,7 +14,7 @@
# local test models
from models import (Article, BarAccount, CustomArticle, EmptyModel,
FooAccount, Gallery, ModelWithStringPrimaryKey,
- Persona, Picture, Section,
+ Person, Persona, Picture, Section,
Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit)
try:
@@ -229,6 +229,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[-1]['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[-1]['form_url'], '../add/')
+
def get_perm(Model, perm):
"""Return the permission object, for the Model"""
ct = ContentType.objects.get_for_model(Model)
Please sign in to comment.
Something went wrong with that request. Please try again.