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
This commit is contained in:
Carl Meyer 2011-09-21 14:00:58 +00:00
parent b22a4fdc71
commit 09a01435de
4 changed files with 55 additions and 5 deletions

View File

@ -926,7 +926,7 @@ class ModelAdmin(BaseModelAdmin):
for FormSet, inline in zip(self.get_formsets(request), self.inline_instances): for FormSet, inline in zip(self.get_formsets(request), self.inline_instances):
prefix = FormSet.get_default_prefix() prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1 prefixes[prefix] = prefixes.get(prefix, 0) + 1
if prefixes[prefix] != 1: if prefixes[prefix] != 1 or not prefix:
prefix = "%s-%s" % (prefix, prefixes[prefix]) prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(data=request.POST, files=request.FILES, formset = FormSet(data=request.POST, files=request.FILES,
instance=new_object, instance=new_object,
@ -955,7 +955,7 @@ class ModelAdmin(BaseModelAdmin):
self.inline_instances): self.inline_instances):
prefix = FormSet.get_default_prefix() prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1 prefixes[prefix] = prefixes.get(prefix, 0) + 1
if prefixes[prefix] != 1: if prefixes[prefix] != 1 or not prefix:
prefix = "%s-%s" % (prefix, prefixes[prefix]) prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(instance=self.model(), prefix=prefix, formset = FormSet(instance=self.model(), prefix=prefix,
queryset=inline.queryset(request)) queryset=inline.queryset(request))
@ -1025,7 +1025,7 @@ class ModelAdmin(BaseModelAdmin):
self.inline_instances): self.inline_instances):
prefix = FormSet.get_default_prefix() prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1 prefixes[prefix] = prefixes.get(prefix, 0) + 1
if prefixes[prefix] != 1: if prefixes[prefix] != 1 or not prefix:
prefix = "%s-%s" % (prefix, prefixes[prefix]) prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(request.POST, request.FILES, formset = FormSet(request.POST, request.FILES,
instance=new_object, prefix=prefix, 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): for FormSet, inline in zip(self.get_formsets(request, obj), self.inline_instances):
prefix = FormSet.get_default_prefix() prefix = FormSet.get_default_prefix()
prefixes[prefix] = prefixes.get(prefix, 0) + 1 prefixes[prefix] = prefixes.get(prefix, 0) + 1
if prefixes[prefix] != 1: if prefixes[prefix] != 1 or not prefix:
prefix = "%s-%s" % (prefix, prefixes[prefix]) prefix = "%s-%s" % (prefix, prefixes[prefix])
formset = FormSet(instance=obj, prefix=prefix, formset = FormSet(instance=obj, prefix=prefix,
queryset=inline.queryset(request)) queryset=inline.queryset(request))

View File

@ -101,6 +101,14 @@ class NovelAdmin(admin.ModelAdmin):
inlines = [ChapterInline] inlines = [ChapterInline]
class ConsigliereInline(admin.TabularInline):
model = Consigliere
class SottoCapoInline(admin.TabularInline):
model = SottoCapo
site.register(TitleCollection, inlines=[TitleInline]) site.register(TitleCollection, inlines=[TitleInline])
# Test bug #12561 and #12778 # Test bug #12561 and #12778
# only ModelAdmin media # only ModelAdmin media
@ -115,3 +123,4 @@ site.register(Novel, NovelAdmin)
site.register(Fashionista, inlines=[InlineWeakness]) site.register(Fashionista, inlines=[InlineWeakness])
site.register(Holder4, Holder4Admin) site.register(Holder4, Holder4Admin)
site.register(Author, AuthorAdmin) site.register(Author, AuthorAdmin)
site.register(CapoFamiglia, inlines=[ConsigliereInline, SottoCapoInline])

View File

@ -122,3 +122,17 @@ class Novel(models.Model):
class Chapter(models.Model): class Chapter(models.Model):
novel = models.ForeignKey(Novel) 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 File

@ -4,7 +4,8 @@ from django.test import TestCase
# local test models # local test models
from models import (Holder, Inner, Holder2, Inner2, Holder3, 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 from admin import InnerInline
@ -115,6 +116,32 @@ class TestInline(TestCase):
self.assertContains(response, '<p class="help">Awesome stacked help text is awesome.</p>', 4) 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) 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): class TestInlineMedia(TestCase):
urls = "regressiontests.admin_inlines.urls" urls = "regressiontests.admin_inlines.urls"
fixtures = ['admin-views-users.xml'] fixtures = ['admin-views-users.xml']