Code

Ticket #18263: 18263.diff

File 18263.diff, 4.6 KB (added by hvdklauw, 23 months ago)

Added regression test

Line 
1diff --git a/django/contrib/admin/templates/admin/edit_inline/tabular.html b/django/contrib/admin/templates/admin/edit_inline/tabular.html
2index d5ac9b0..760bae7 100644
3--- a/django/contrib/admin/templates/admin/edit_inline/tabular.html
4+++ b/django/contrib/admin/templates/admin/edit_inline/tabular.html
5@@ -35,7 +35,7 @@
6           {% for fieldset in inline_admin_form %}
7             {% for line in fieldset %}
8               {% for field in line %}
9-                {% if field.is_hidden %} {{ field.field }} {% endif %}
10+                {% if field.field.is_hidden %} {{ field.field }} {% endif %}
11               {% endfor %}
12             {% endfor %}
13           {% endfor %}
14@@ -44,6 +44,7 @@
15         {% for fieldset in inline_admin_form %}
16           {% for line in fieldset %}
17             {% for field in line %}
18+              {% if not field.field.is_hidden %}
19               <td{% if field.field.name %} class="field-{{ field.field.name }}"{% endif %}>
20               {% if field.is_readonly %}
21                   <p>{{ field.contents }}</p>
22@@ -52,6 +53,7 @@
23                   {{ field.field }}
24               {% endif %}
25               </td>
26+              {% endif %}
27             {% endfor %}
28           {% endfor %}
29         {% endfor %}
30diff --git a/tests/regressiontests/admin_inlines/admin.py b/tests/regressiontests/admin_inlines/admin.py
31index cf51fa4..6b51ad2 100644
32--- a/tests/regressiontests/admin_inlines/admin.py
33+++ b/tests/regressiontests/admin_inlines/admin.py
34@@ -125,6 +125,21 @@ class ChildModel2Inline(admin.StackedInline):
35     model = ChildModel2
36 
37 
38+#admin and form for #18263
39+class SomeChildModelForm(forms.ModelForm):
40+
41+    class Meta:
42+        model = SomeChildModel
43+        widgets = {
44+            'position': forms.HiddenInput,
45+        }
46+
47+
48+class SomeChildModelInline(admin.TabularInline):
49+    model = SomeChildModel
50+    form = SomeChildModelForm
51+
52+
53 site.register(TitleCollection, inlines=[TitleInline])
54 # Test bug #12561 and #12778
55 # only ModelAdmin media
56@@ -141,4 +156,5 @@ site.register(Holder4, Holder4Admin)
57 site.register(Author, AuthorAdmin)
58 site.register(CapoFamiglia, inlines=[ConsigliereInline, SottoCapoInline])
59 site.register(ProfileCollection, inlines=[ProfileInline])
60-site.register(ParentModelWithCustomPk, inlines=[ChildModel1Inline, ChildModel2Inline])
61\ No newline at end of file
62+site.register(ParentModelWithCustomPk, inlines=[ChildModel1Inline, ChildModel2Inline])
63+site.register(SomeParentModel, inlines=[SomeChildModelInline])
64diff --git a/tests/regressiontests/admin_inlines/models.py b/tests/regressiontests/admin_inlines/models.py
65index 5a0f4d8..6cafa3b 100644
66--- a/tests/regressiontests/admin_inlines/models.py
67+++ b/tests/regressiontests/admin_inlines/models.py
68@@ -164,6 +164,17 @@ class ChildModel2(models.Model):
69         return '/child_model2/'
70 
71 
72+# Models for #18263
73+
74+class SomeParentModel(models.Model):
75+    name = models.CharField(max_length=1)
76+
77+
78+class SomeChildModel(models.Model):
79+    name = models.CharField(max_length=1)
80+    position = models.PositiveIntegerField()
81+    parent = models.ForeignKey(SomeParentModel)
82+
83 # Other models
84 
85 class ProfileCollection(models.Model):
86diff --git a/tests/regressiontests/admin_inlines/tests.py b/tests/regressiontests/admin_inlines/tests.py
87index 4f25d3d..f904479 100644
88--- a/tests/regressiontests/admin_inlines/tests.py
89+++ b/tests/regressiontests/admin_inlines/tests.py
90@@ -11,7 +11,8 @@ from django.test.utils import override_settings
91 from .admin import InnerInline
92 from .models import (Holder, Inner, Holder2, Inner2, Holder3, Inner3, Person,
93     OutfitItem, Fashionista, Teacher, Parent, Child, Author, Book, Profile,
94-    ProfileCollection, ParentModelWithCustomPk, ChildModel1, ChildModel2)
95+    ProfileCollection, ParentModelWithCustomPk, ChildModel1, ChildModel2,
96+    SomeParentModel, SomeChildModel)
97 
98 
99 @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
100@@ -161,6 +162,14 @@ class TestInline(TestCase):
101         self.assertContains(response, child1_shortcut)
102         self.assertContains(response, child2_shortcut)
103 
104+    def test_inline_hidden_field_no_column(self):
105+        """Make sure hidden fields don't get a column in tabular inlines"""
106+        parent = SomeParentModel.objects.create(name='a')
107+        SomeChildModel.objects.create(name='b', position='0', parent=parent)
108+        SomeChildModel.objects.create(name='c', position='1', parent=parent)
109+        response = self.client.get('/admin/admin_inlines/someparentmodel/%s/' % parent.pk)
110+        self.assertNotContains(response, '<td class="field-position">')
111+
112 
113 @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
114 class TestInlineMedia(TestCase):