Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Carl Meyer authored September 21, 2011
8  django/contrib/admin/options.py
@@ -926,7 +926,7 @@ def add_view(self, request, form_url='', extra_context=None):
926 926
             for FormSet, inline in zip(self.get_formsets(request), self.inline_instances):
927 927
                 prefix = FormSet.get_default_prefix()
928 928
                 prefixes[prefix] = prefixes.get(prefix, 0) + 1
929  
-                if prefixes[prefix] != 1:
  929
+                if prefixes[prefix] != 1 or not prefix:
930 930
                     prefix = "%s-%s" % (prefix, prefixes[prefix])
931 931
                 formset = FormSet(data=request.POST, files=request.FILES,
932 932
                                   instance=new_object,
@@ -955,7 +955,7 @@ def add_view(self, request, form_url='', extra_context=None):
955 955
                                        self.inline_instances):
956 956
                 prefix = FormSet.get_default_prefix()
957 957
                 prefixes[prefix] = prefixes.get(prefix, 0) + 1
958  
-                if prefixes[prefix] != 1:
  958
+                if prefixes[prefix] != 1 or not prefix:
959 959
                     prefix = "%s-%s" % (prefix, prefixes[prefix])
960 960
                 formset = FormSet(instance=self.model(), prefix=prefix,
961 961
                                   queryset=inline.queryset(request))
@@ -1025,7 +1025,7 @@ def change_view(self, request, object_id, extra_context=None):
1025 1025
                                        self.inline_instances):
1026 1026
                 prefix = FormSet.get_default_prefix()
1027 1027
                 prefixes[prefix] = prefixes.get(prefix, 0) + 1
1028  
-                if prefixes[prefix] != 1:
  1028
+                if prefixes[prefix] != 1 or not prefix:
1029 1029
                     prefix = "%s-%s" % (prefix, prefixes[prefix])
1030 1030
                 formset = FormSet(request.POST, request.FILES,
1031 1031
                                   instance=new_object, prefix=prefix,
@@ -1046,7 +1046,7 @@ def change_view(self, request, object_id, extra_context=None):
1046 1046
             for FormSet, inline in zip(self.get_formsets(request, obj), self.inline_instances):
1047 1047
                 prefix = FormSet.get_default_prefix()
1048 1048
                 prefixes[prefix] = prefixes.get(prefix, 0) + 1
1049  
-                if prefixes[prefix] != 1:
  1049
+                if prefixes[prefix] != 1 or not prefix:
1050 1050
                     prefix = "%s-%s" % (prefix, prefixes[prefix])
1051 1051
                 formset = FormSet(instance=obj, prefix=prefix,
1052 1052
                                   queryset=inline.queryset(request))
9  tests/regressiontests/admin_inlines/admin.py
@@ -101,6 +101,14 @@ class NovelAdmin(admin.ModelAdmin):
101 101
     inlines = [ChapterInline]
102 102
 
103 103
 
  104
+class ConsigliereInline(admin.TabularInline):
  105
+    model = Consigliere
  106
+
  107
+
  108
+class SottoCapoInline(admin.TabularInline):
  109
+    model = SottoCapo
  110
+
  111
+
104 112
 site.register(TitleCollection, inlines=[TitleInline])
105 113
 # Test bug #12561 and #12778
106 114
 # only ModelAdmin media
@@ -115,3 +123,4 @@ class NovelAdmin(admin.ModelAdmin):
115 123
 site.register(Fashionista, inlines=[InlineWeakness])
116 124
 site.register(Holder4, Holder4Admin)
117 125
 site.register(Author, AuthorAdmin)
  126
+site.register(CapoFamiglia, inlines=[ConsigliereInline, SottoCapoInline])
14  tests/regressiontests/admin_inlines/models.py
@@ -122,3 +122,17 @@ class Novel(models.Model):
122 122
 class Chapter(models.Model):
123 123
     novel = models.ForeignKey(Novel)
124 124
 
  125
+
  126
+# Models for #16838
  127
+class CapoFamiglia(models.Model):
  128
+    name = models.CharField(max_length=100)
  129
+
  130
+
  131
+class Consigliere(models.Model):
  132
+    name = models.CharField(max_length=100)
  133
+    capo_famiglia = models.ForeignKey(CapoFamiglia, related_name='+')
  134
+
  135
+
  136
+class SottoCapo(models.Model):
  137
+    name = models.CharField(max_length=100)
  138
+    capo_famiglia = models.ForeignKey(CapoFamiglia, related_name='+')
29  tests/regressiontests/admin_inlines/tests.py
@@ -4,7 +4,8 @@
4 4
 
5 5
 # local test models
6 6
 from 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)
8 9
 from admin import InnerInline
9 10
 
10 11
 
@@ -115,6 +116,32 @@ def test_help_text(self):
115 116
         self.assertContains(response, '<p class="help">Awesome stacked help text is awesome.</p>', 4)
116 117
         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)
117 118
 
  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
+
118 145
 class TestInlineMedia(TestCase):
119 146
     urls = "regressiontests.admin_inlines.urls"
120 147
     fixtures = ['admin-views-users.xml']

0 notes on commit 09a0143

Please sign in to comment.
Something went wrong with that request. Please try again.