Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.1.X] Fixed #12561. InlineAdmin now respects can_delete=False. Back…

…port of [12533] from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12534 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 651082267956cef3bf3dbd48c5314046c14ca01c 1 parent c7117b1
Joseph Kocherhans jkocherhans authored
2  AUTHORS
View
@@ -68,7 +68,7 @@ answer newbie questions, and generally made Django that much better:
James Bennett
Julian Bez
Arvis Bickovskis <viestards.lists@gmail.com>
- Natalia Bidart
+ Natalia Bidart <nataliabidart@gmail.com>
Paul Bissex <http://e-scribe.com/>
Simon Blanchard
David Blewett <david@dawninglight.net>
2  django/contrib/admin/options.py
View
@@ -1121,6 +1121,7 @@ class InlineModelAdmin(BaseModelAdmin):
template = None
verbose_name = None
verbose_name_plural = None
+ can_delete = True
def __init__(self, parent_model, admin_site):
self.admin_site = admin_site
@@ -1163,6 +1164,7 @@ def get_formset(self, request, obj=None, **kwargs):
"formfield_callback": curry(self.formfield_for_dbfield, request=request),
"extra": self.extra,
"max_num": self.max_num,
+ "can_delete": self.can_delete,
}
defaults.update(kwargs)
return inlineformset_factory(self.parent_model, self.model, **defaults)
2  django/contrib/contenttypes/generic.py
View
@@ -386,7 +386,7 @@ def get_formset(self, request, obj=None):
"formfield_callback": self.formfield_for_dbfield,
"formset": self.formset,
"extra": self.extra,
- "can_delete": True,
+ "can_delete": self.can_delete,
"can_order": False,
"fields": fields,
"max_num": self.max_num,
17 tests/regressiontests/admin_inlines/fixtures/admin-views-users.xml
View
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<django-objects version="1.0">
+ <object pk="100" model="auth.user">
+ <field type="CharField" name="username">super</field>
+ <field type="CharField" name="first_name">Super</field>
+ <field type="CharField" name="last_name">User</field>
+ <field type="CharField" name="email">super@example.com</field>
+ <field type="CharField" name="password">sha1$995a3$6011485ea3834267d719b4c801409b8b1ddd0158</field>
+ <field type="BooleanField" name="is_staff">True</field>
+ <field type="BooleanField" name="is_active">True</field>
+ <field type="BooleanField" name="is_superuser">True</field>
+ <field type="DateTimeField" name="last_login">2007-05-30 13:20:10</field>
+ <field type="DateTimeField" name="date_joined">2007-05-30 13:20:10</field>
+ <field to="auth.group" name="groups" rel="ManyToManyRel"></field>
+ <field to="auth.permission" name="user_permissions" rel="ManyToManyRel"></field>
+ </object>
+</django-objects>
21 tests/regressiontests/admin_inlines/models.py
View
@@ -3,6 +3,7 @@
"""
from django.db import models
+from django.contrib import admin
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
@@ -29,6 +30,24 @@ class Child(models.Model):
def __unicode__(self):
return u'I am %s, a child of %s' % (self.name, self.parent)
+
+class Holder(models.Model):
+ dummy = models.IntegerField()
+
+
+class Inner(models.Model):
+ dummy = models.IntegerField()
+ holder = models.ForeignKey(Holder)
+
+
+class InnerInline(admin.StackedInline):
+ model = Inner
+ can_delete = False
+
+
+# Test bug #12561
+admin.site.register(Holder, inlines=[InnerInline])
+
__test__ = {'API_TESTS': """
# Regression test for #9362
@@ -48,4 +67,4 @@ def __unicode__(self):
<Child: I am Joe, a child of John>
"""
-}
+}
30 tests/regressiontests/admin_inlines/tests.py
View
@@ -0,0 +1,30 @@
+from django.test import TestCase
+
+# local test models
+from models import Holder, Inner, InnerInline
+
+class TestInline(TestCase):
+ fixtures = ['admin-views-users.xml']
+
+ def setUp(self):
+ holder = Holder(dummy=13)
+ holder.save()
+ Inner(dummy=42, holder=holder).save()
+ self.change_url = '/test_admin/admin/admin_inlines/holder/%i/' % holder.id
+
+ result = self.client.login(username='super', password='secret')
+ self.failUnlessEqual(result, True)
+
+ def tearDown(self):
+ self.client.logout()
+
+ def test_can_delete(self):
+ """
+ can_delete should be passed to inlineformset factory.
+ """
+ response = self.client.get(self.change_url)
+ inner_formset = response.context[-1]['inline_admin_formsets'][0].formset
+ expected = InnerInline.can_delete
+ actual = inner_formset.can_delete
+ self.assertEqual(expected, actual, 'can_delete must be equal')
+
12 tests/regressiontests/generic_inline_admin/models.py
View
@@ -72,3 +72,15 @@ class MediaExcludeInline(generic.GenericTabularInline):
admin.site.register(EpisodeExclude, inlines=[MediaExcludeInline])
+#
+# Generic inline with can_delete=False
+#
+
+class EpisodePermanent(Episode):
+ pass
+
+class MediaPermanentInline(generic.GenericTabularInline):
+ model = Media
+ can_delete = False
+
+admin.site.register(EpisodePermanent, inlines=[MediaPermanentInline])
11 tests/regressiontests/generic_inline_admin/tests.py
View
@@ -5,7 +5,8 @@
from django.contrib.contenttypes.generic import generic_inlineformset_factory
# local test models
-from models import Episode, EpisodeExtra, EpisodeMaxNum, EpisodeExclude, Media
+from models import Episode, EpisodeExtra, EpisodeMaxNum, EpisodeExclude, \
+ Media, EpisodePermanent, MediaPermanentInline
class GenericAdminViewTest(TestCase):
fixtures = ['users.xml']
@@ -177,3 +178,11 @@ def testExcludeParam(self):
response = self.client.get('/generic_inline_admin/admin/generic_inline_admin/episodeexclude/%s/' % e.pk)
formset = response.context['inline_admin_formsets'][0].formset
self.failIf('url' in formset.forms[0], 'The formset has excluded "url" field.')
+
+class NoInlineDeletionTest(TestCase):
+ def test_no_deletion(self):
+ fake_site = object()
+ inline = MediaPermanentInline(EpisodePermanent, fake_site)
+ fake_request = object()
+ formset = inline.get_formset(fake_request)
+ self.assertFalse(formset.can_delete)
1  tests/regressiontests/modeladmin/models.py
View
@@ -291,6 +291,7 @@ class and an AdminSite instance, so let's just go ahead and do that manually
... form = AdminConcertForm
... model = Concert
... fk_name = 'main_band'
+... can_delete = True
>>> class BandAdmin(ModelAdmin):
... inlines = [
Please sign in to comment.
Something went wrong with that request. Please try again.