From 617aceb1b4ec97b4d70f9b3071cb46a645f69f87 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 7 Oct 2013 12:30:02 -0400 Subject: [PATCH] Fixed #18263 -- Corrrected handling of hidden fields in tabular admin inlines. Thanks hvdklauw for the report and patch. --- .../templates/admin/edit_inline/tabular.html | 4 +++- tests/admin_inlines/admin.py | 17 +++++++++++++++++ tests/admin_inlines/models.py | 11 +++++++++++ tests/admin_inlines/tests.py | 13 ++++++++++++- 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/django/contrib/admin/templates/admin/edit_inline/tabular.html b/django/contrib/admin/templates/admin/edit_inline/tabular.html index e6097f0dce0..0bfa405272f 100644 --- a/django/contrib/admin/templates/admin/edit_inline/tabular.html +++ b/django/contrib/admin/templates/admin/edit_inline/tabular.html @@ -35,7 +35,7 @@ {% for fieldset in inline_admin_form %} {% for line in fieldset %} {% for field in line %} - {% if field.is_hidden %} {{ field.field }} {% endif %} + {% if field.field.is_hidden %} {{ field.field }} {% endif %} {% endfor %} {% endfor %} {% endfor %} @@ -44,6 +44,7 @@ {% for fieldset in inline_admin_form %} {% for line in fieldset %} {% for field in line %} + {% if not field.field.is_hidden %} {% if field.is_readonly %}

{{ field.contents|linebreaksbr }}

@@ -52,6 +53,7 @@ {{ field.field }} {% endif %} + {% endif %} {% endfor %} {% endfor %} {% endfor %} diff --git a/tests/admin_inlines/admin.py b/tests/admin_inlines/admin.py index c69800851a7..c141abe54ef 100644 --- a/tests/admin_inlines/admin.py +++ b/tests/admin_inlines/admin.py @@ -165,6 +165,22 @@ class BinaryTreeAdmin(admin.TabularInline): class SightingInline(admin.TabularInline): model = Sighting +# admin and form for #18263 +class SomeChildModelForm(forms.ModelForm): + + class Meta: + fields = '__all__' + model = SomeChildModel + widgets = { + 'position': forms.HiddenInput, + } + + +class SomeChildModelInline(admin.TabularInline): + model = SomeChildModel + form = SomeChildModelForm + + site.register(TitleCollection, inlines=[TitleInline]) # Test bug #12561 and #12778 # only ModelAdmin media @@ -184,3 +200,4 @@ site.register(ProfileCollection, inlines=[ProfileInline]) site.register(ParentModelWithCustomPk, inlines=[ChildModel1Inline, ChildModel2Inline]) site.register(BinaryTree, inlines=[BinaryTreeAdmin]) site.register(ExtraTerrestrial, inlines=[SightingInline]) +site.register(SomeParentModel, inlines=[SomeChildModelInline]) diff --git a/tests/admin_inlines/models.py b/tests/admin_inlines/models.py index dde5cf79e30..36e9e8e54df 100644 --- a/tests/admin_inlines/models.py +++ b/tests/admin_inlines/models.py @@ -221,6 +221,17 @@ class Sighting(models.Model): et = models.ForeignKey(ExtraTerrestrial) place = models.CharField(max_length=100) + +# Models for #18263 +class SomeParentModel(models.Model): + name = models.CharField(max_length=1) + + +class SomeChildModel(models.Model): + name = models.CharField(max_length=1) + position = models.PositiveIntegerField() + parent = models.ForeignKey(SomeParentModel) + # Other models class ProfileCollection(models.Model): diff --git a/tests/admin_inlines/tests.py b/tests/admin_inlines/tests.py index b7765b7f86e..871852425aa 100644 --- a/tests/admin_inlines/tests.py +++ b/tests/admin_inlines/tests.py @@ -12,7 +12,8 @@ from .admin import InnerInline from .models import (Holder, Inner, Holder2, Inner2, Holder3, Inner3, Person, OutfitItem, Fashionista, Teacher, Parent, Child, Author, Book, Profile, ProfileCollection, ParentModelWithCustomPk, ChildModel1, ChildModel2, - Sighting, Novel, Chapter, FootNote, BinaryTree) + Sighting, Novel, Chapter, FootNote, BinaryTree, SomeParentModel, + SomeChildModel) @override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',)) @@ -126,6 +127,16 @@ class TestInline(TestCase): response = self.client.get('/admin/admin_inlines/capofamiglia/add/') self.assertContains(response, '(Help text for ReadOnlyInline)', 1) + def test_inline_hidden_field_no_column(self): + """#18263 -- Make sure hidden fields don't get a column in tabular inlines""" + parent = SomeParentModel.objects.create(name='a') + SomeChildModel.objects.create(name='b', position='0', parent=parent) + SomeChildModel.objects.create(name='c', position='1', parent=parent) + response = self.client.get('/admin/admin_inlines/someparentmodel/%s/' % parent.pk) + self.assertNotContains(response, '') + self.assertContains(response, ( + '')) def test_non_related_name_inline(self): """