From 09a01435de65989864d56a8bf74ff2d84e9c4bd5 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Wed, 21 Sep 2011 14:00:58 +0000 Subject: [PATCH] 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 --- django/contrib/admin/options.py | 8 ++--- tests/regressiontests/admin_inlines/admin.py | 9 ++++++ tests/regressiontests/admin_inlines/models.py | 14 +++++++++ tests/regressiontests/admin_inlines/tests.py | 29 ++++++++++++++++++- 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py index 2825c99988..f05b5cb3a7 100644 --- a/django/contrib/admin/options.py +++ b/django/contrib/admin/options.py @@ -926,7 +926,7 @@ class ModelAdmin(BaseModelAdmin): 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 @@ class ModelAdmin(BaseModelAdmin): 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 @@ class ModelAdmin(BaseModelAdmin): 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 @@ class ModelAdmin(BaseModelAdmin): 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)) diff --git a/tests/regressiontests/admin_inlines/admin.py b/tests/regressiontests/admin_inlines/admin.py index 6f8076abfa..4edd361d09 100644 --- a/tests/regressiontests/admin_inlines/admin.py +++ b/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 @@ site.register(Novel, NovelAdmin) site.register(Fashionista, inlines=[InlineWeakness]) site.register(Holder4, Holder4Admin) site.register(Author, AuthorAdmin) +site.register(CapoFamiglia, inlines=[ConsigliereInline, SottoCapoInline]) diff --git a/tests/regressiontests/admin_inlines/models.py b/tests/regressiontests/admin_inlines/models.py index f4e58f2f28..748280d8ab 100644 --- a/tests/regressiontests/admin_inlines/models.py +++ b/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='+') diff --git a/tests/regressiontests/admin_inlines/tests.py b/tests/regressiontests/admin_inlines/tests.py index efa6cf6934..955d6208de 100644 --- a/tests/regressiontests/admin_inlines/tests.py +++ b/tests/regressiontests/admin_inlines/tests.py @@ -4,7 +4,8 @@ from django.test import TestCase # 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 @@ class TestInline(TestCase): self.assertContains(response, '

Awesome stacked help text is awesome.

', 4) self.assertContains(response, '(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, + '') + self.assertContains(response, + '') + self.assertContains(response, + '') + + self.assertContains(response, + '') + self.assertContains(response, + '') + self.assertContains(response, + '') + + class TestInlineMedia(TestCase): urls = "regressiontests.admin_inlines.urls" fixtures = ['admin-views-users.xml']