Ticket #16838: t16838-potential_fix.diff

File t16838-potential_fix.diff, 6.1 KB (added by jamesp, 13 years ago)

Potential fix for this bug

  • django/contrib/admin/options.py

    diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
    index 2825c99..f05b5cb 100644
    a b class ModelAdmin(BaseModelAdmin):  
    926926            for FormSet, inline in zip(self.get_formsets(request), self.inline_instances):
    927927                prefix = FormSet.get_default_prefix()
    928928                prefixes[prefix] = prefixes.get(prefix, 0) + 1
    929                 if prefixes[prefix] != 1:
     929                if prefixes[prefix] != 1 or not prefix:
    930930                    prefix = "%s-%s" % (prefix, prefixes[prefix])
    931931                formset = FormSet(data=request.POST, files=request.FILES,
    932932                                  instance=new_object,
    class ModelAdmin(BaseModelAdmin):  
    955955                                       self.inline_instances):
    956956                prefix = FormSet.get_default_prefix()
    957957                prefixes[prefix] = prefixes.get(prefix, 0) + 1
    958                 if prefixes[prefix] != 1:
     958                if prefixes[prefix] != 1 or not prefix:
    959959                    prefix = "%s-%s" % (prefix, prefixes[prefix])
    960960                formset = FormSet(instance=self.model(), prefix=prefix,
    961961                                  queryset=inline.queryset(request))
    class ModelAdmin(BaseModelAdmin):  
    10251025                                       self.inline_instances):
    10261026                prefix = FormSet.get_default_prefix()
    10271027                prefixes[prefix] = prefixes.get(prefix, 0) + 1
    1028                 if prefixes[prefix] != 1:
     1028                if prefixes[prefix] != 1 or not prefix:
    10291029                    prefix = "%s-%s" % (prefix, prefixes[prefix])
    10301030                formset = FormSet(request.POST, request.FILES,
    10311031                                  instance=new_object, prefix=prefix,
    class ModelAdmin(BaseModelAdmin):  
    10461046            for FormSet, inline in zip(self.get_formsets(request, obj), self.inline_instances):
    10471047                prefix = FormSet.get_default_prefix()
    10481048                prefixes[prefix] = prefixes.get(prefix, 0) + 1
    1049                 if prefixes[prefix] != 1:
     1049                if prefixes[prefix] != 1 or not prefix:
    10501050                    prefix = "%s-%s" % (prefix, prefixes[prefix])
    10511051                formset = FormSet(instance=obj, prefix=prefix,
    10521052                                  queryset=inline.queryset(request))
  • tests/regressiontests/admin_inlines/admin.py

    diff --git a/tests/regressiontests/admin_inlines/admin.py b/tests/regressiontests/admin_inlines/admin.py
    index 6f8076a..4edd361 100644
    a b class NovelAdmin(admin.ModelAdmin):  
    101101    inlines = [ChapterInline]
    102102
    103103
     104class ConsigliereInline(admin.TabularInline):
     105    model = Consigliere
     106
     107
     108class SottoCapoInline(admin.TabularInline):
     109    model = SottoCapo
     110
     111
    104112site.register(TitleCollection, inlines=[TitleInline])
    105113# Test bug #12561 and #12778
    106114# only ModelAdmin media
    site.register(Novel, NovelAdmin)  
    115123site.register(Fashionista, inlines=[InlineWeakness])
    116124site.register(Holder4, Holder4Admin)
    117125site.register(Author, AuthorAdmin)
     126site.register(CapoFamiglia, inlines=[ConsigliereInline, SottoCapoInline])
  • tests/regressiontests/admin_inlines/models.py

    diff --git a/tests/regressiontests/admin_inlines/models.py b/tests/regressiontests/admin_inlines/models.py
    index f4e58f2..748280d 100644
    a b class Novel(models.Model):  
    122122class Chapter(models.Model):
    123123    novel = models.ForeignKey(Novel)
    124124
     125
     126# Models for #16838
     127class CapoFamiglia(models.Model):
     128    name = models.CharField(max_length=100)
     129
     130
     131class Consigliere(models.Model):
     132    name = models.CharField(max_length=100)
     133    capo_famiglia = models.ForeignKey(CapoFamiglia, related_name='+')
     134
     135
     136class SottoCapo(models.Model):
     137    name = models.CharField(max_length=100)
     138    capo_famiglia = models.ForeignKey(CapoFamiglia, related_name='+')
  • tests/regressiontests/admin_inlines/tests.py

    diff --git a/tests/regressiontests/admin_inlines/tests.py b/tests/regressiontests/admin_inlines/tests.py
    index efa6cf6..955d620 100644
    a b from django.test import TestCase  
    44
    55# local test models
    66from models import (Holder, Inner, Holder2, Inner2, Holder3,
    7     Inner3, Person, OutfitItem, Fashionista, Teacher, Parent, Child)
     7    Inner3, Person, OutfitItem, Fashionista, Teacher, Parent, Child,
     8    CapoFamiglia, Consigliere, SottoCapo)
    89from admin import InnerInline
    910
    1011
    class TestInline(TestCase):  
    115116        self.assertContains(response, '<p class="help">Awesome stacked help text is awesome.</p>', 4)
    116117        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)
    117118
     119    def test_non_related_name_inline(self):
     120        """
     121        Ensure that multiple inlines with related_name='+' have correct form
     122        prefixes. Bug #16838.
     123        """
     124        response = self.client.get('/admin/admin_inlines/capofamiglia/add/')
     125
     126        self.assertContains(response,
     127                '<input type="hidden" name="-1-0-id" id="id_-1-0-id" />')
     128        self.assertContains(response,
     129                '<input type="hidden" name="-1-0-capo_famiglia" '
     130                'id="id_-1-0-capo_famiglia" />')
     131        self.assertContains(response,
     132                '<input id="id_-1-0-name" type="text" class="vTextField" '
     133                'name="-1-0-name" maxlength="100" />')
     134
     135        self.assertContains(response,
     136                '<input type="hidden" name="-2-0-id" id="id_-2-0-id" />')
     137        self.assertContains(response,
     138                '<input type="hidden" name="-2-0-capo_famiglia" '
     139                'id="id_-2-0-capo_famiglia" />')
     140        self.assertContains(response,
     141                '<input id="id_-2-0-name" type="text" class="vTextField" '
     142                'name="-2-0-name" maxlength="100" />')
     143
     144
    118145class TestInlineMedia(TestCase):
    119146    urls = "regressiontests.admin_inlines.urls"
    120147    fixtures = ['admin-views-users.xml']
Back to Top