Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #16838 -- Corrected broken add-another inline JS in admin with …

…related_name="+". Thanks jamesp for report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16860 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 09a01435de65989864d56a8bf74ff2d84e9c4bd5 1 parent b22a4fd
@carljm carljm authored
View
8 django/contrib/admin/options.py
@@ -926,7 +926,7 @@ def add_view(self, request, form_url='', extra_context=None):
for FormSet, inline in zip(self.get_formsets(request), self.inline_instances):
prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1
- if prefixes[prefix] != 1:
+ if prefixes[prefix] != 1 or not prefix:
prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(data=request.POST, files=request.FILES,
instance=new_object,
@@ -955,7 +955,7 @@ def add_view(self, request, form_url='', extra_context=None):
self.inline_instances):
prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1
- if prefixes[prefix] != 1:
+ if prefixes[prefix] != 1 or not prefix:
prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(instance=self.model(), prefix=prefix,
queryset=inline.queryset(request))
@@ -1025,7 +1025,7 @@ def change_view(self, request, object_id, extra_context=None):
self.inline_instances):
prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1
- if prefixes[prefix] != 1:
+ if prefixes[prefix] != 1 or not prefix:
prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(request.POST, request.FILES,
instance=new_object, prefix=prefix,
@@ -1046,7 +1046,7 @@ def change_view(self, request, object_id, extra_context=None):
for FormSet, inline in zip(self.get_formsets(request, obj), self.inline_instances):
prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1
- if prefixes[prefix] != 1:
+ if prefixes[prefix] != 1 or not prefix:
prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(instance=obj, prefix=prefix,
queryset=inline.queryset(request))
View
9 tests/regressiontests/admin_inlines/admin.py
@@ -101,6 +101,14 @@ class NovelAdmin(admin.ModelAdmin):
inlines = [ChapterInline]
+class ConsigliereInline(admin.TabularInline):
+ model = Consigliere
+
+
+class SottoCapoInline(admin.TabularInline):
+ model = SottoCapo
+
+
site.register(TitleCollection, inlines=[TitleInline])
# Test bug #12561 and #12778
# only ModelAdmin media
@@ -115,3 +123,4 @@ class NovelAdmin(admin.ModelAdmin):
site.register(Fashionista, inlines=[InlineWeakness])
site.register(Holder4, Holder4Admin)
site.register(Author, AuthorAdmin)
+site.register(CapoFamiglia, inlines=[ConsigliereInline, SottoCapoInline])
View
14 tests/regressiontests/admin_inlines/models.py
@@ -122,3 +122,17 @@ class Novel(models.Model):
class Chapter(models.Model):
novel = models.ForeignKey(Novel)
+
+# Models for #16838
+class CapoFamiglia(models.Model):
+ name = models.CharField(max_length=100)
+
+
+class Consigliere(models.Model):
+ name = models.CharField(max_length=100)
+ capo_famiglia = models.ForeignKey(CapoFamiglia, related_name='+')
+
+
+class SottoCapo(models.Model):
+ name = models.CharField(max_length=100)
+ capo_famiglia = models.ForeignKey(CapoFamiglia, related_name='+')
View
29 tests/regressiontests/admin_inlines/tests.py
@@ -4,7 +4,8 @@
# local test models
from models import (Holder, Inner, Holder2, Inner2, Holder3,
- Inner3, Person, OutfitItem, Fashionista, Teacher, Parent, Child)
+ Inner3, Person, OutfitItem, Fashionista, Teacher, Parent, Child,
+ CapoFamiglia, Consigliere, SottoCapo)
from admin import InnerInline
@@ -115,6 +116,32 @@ def test_help_text(self):
self.assertContains(response, '<p class="help">Awesome stacked help text is awesome.</p>', 4)
self.assertContains(response, '<img src="/static/admin/img/icon-unknown.gif" class="help help-tooltip" width="10" height="10" alt="(Awesome tabular help text is awesome.)" title="Awesome tabular help text is awesome." />', 1)
+ def test_non_related_name_inline(self):
+ """
+ Ensure that multiple inlines with related_name='+' have correct form
+ prefixes. Bug #16838.
+ """
+ response = self.client.get('/admin/admin_inlines/capofamiglia/add/')
+
+ self.assertContains(response,
+ '<input type="hidden" name="-1-0-id" id="id_-1-0-id" />')
+ self.assertContains(response,
+ '<input type="hidden" name="-1-0-capo_famiglia" '
+ 'id="id_-1-0-capo_famiglia" />')
+ self.assertContains(response,
+ '<input id="id_-1-0-name" type="text" class="vTextField" '
+ 'name="-1-0-name" maxlength="100" />')
+
+ self.assertContains(response,
+ '<input type="hidden" name="-2-0-id" id="id_-2-0-id" />')
+ self.assertContains(response,
+ '<input type="hidden" name="-2-0-capo_famiglia" '
+ 'id="id_-2-0-capo_famiglia" />')
+ self.assertContains(response,
+ '<input id="id_-2-0-name" type="text" class="vTextField" '
+ 'name="-2-0-name" maxlength="100" />')
+
+
class TestInlineMedia(TestCase):
urls = "regressiontests.admin_inlines.urls"
fixtures = ['admin-views-users.xml']
Please sign in to comment.
Something went wrong with that request. Please try again.