Fixed #18263 -- Corrrected handling of hidden fields in tabular admin inlines.

Thanks hvdklauw for the report and patch.
This commit is contained in:
Tim Graham 2013-10-07 12:30:02 -04:00
parent 4dbd95ad65
commit 617aceb1b4
4 changed files with 43 additions and 2 deletions

View File

@ -35,7 +35,7 @@
{% for fieldset in inline_admin_form %} {% for fieldset in inline_admin_form %}
{% for line in fieldset %} {% for line in fieldset %}
{% for field in line %} {% for field in line %}
{% if field.is_hidden %} {{ field.field }} {% endif %} {% if field.field.is_hidden %} {{ field.field }} {% endif %}
{% endfor %} {% endfor %}
{% endfor %} {% endfor %}
{% endfor %} {% endfor %}
@ -44,6 +44,7 @@
{% for fieldset in inline_admin_form %} {% for fieldset in inline_admin_form %}
{% for line in fieldset %} {% for line in fieldset %}
{% for field in line %} {% for field in line %}
{% if not field.field.is_hidden %}
<td{% if field.field.name %} class="field-{{ field.field.name }}"{% endif %}> <td{% if field.field.name %} class="field-{{ field.field.name }}"{% endif %}>
{% if field.is_readonly %} {% if field.is_readonly %}
<p>{{ field.contents|linebreaksbr }}</p> <p>{{ field.contents|linebreaksbr }}</p>
@ -52,6 +53,7 @@
{{ field.field }} {{ field.field }}
{% endif %} {% endif %}
</td> </td>
{% endif %}
{% endfor %} {% endfor %}
{% endfor %} {% endfor %}
{% endfor %} {% endfor %}

View File

@ -165,6 +165,22 @@ class BinaryTreeAdmin(admin.TabularInline):
class SightingInline(admin.TabularInline): class SightingInline(admin.TabularInline):
model = Sighting 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]) site.register(TitleCollection, inlines=[TitleInline])
# Test bug #12561 and #12778 # Test bug #12561 and #12778
# only ModelAdmin media # only ModelAdmin media
@ -184,3 +200,4 @@ site.register(ProfileCollection, inlines=[ProfileInline])
site.register(ParentModelWithCustomPk, inlines=[ChildModel1Inline, ChildModel2Inline]) site.register(ParentModelWithCustomPk, inlines=[ChildModel1Inline, ChildModel2Inline])
site.register(BinaryTree, inlines=[BinaryTreeAdmin]) site.register(BinaryTree, inlines=[BinaryTreeAdmin])
site.register(ExtraTerrestrial, inlines=[SightingInline]) site.register(ExtraTerrestrial, inlines=[SightingInline])
site.register(SomeParentModel, inlines=[SomeChildModelInline])

View File

@ -221,6 +221,17 @@ class Sighting(models.Model):
et = models.ForeignKey(ExtraTerrestrial) et = models.ForeignKey(ExtraTerrestrial)
place = models.CharField(max_length=100) 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 # Other models
class ProfileCollection(models.Model): class ProfileCollection(models.Model):

View File

@ -12,7 +12,8 @@ from .admin import InnerInline
from .models import (Holder, Inner, Holder2, Inner2, Holder3, Inner3, Person, from .models import (Holder, Inner, Holder2, Inner2, Holder3, Inner3, Person,
OutfitItem, Fashionista, Teacher, Parent, Child, Author, Book, Profile, OutfitItem, Fashionista, Teacher, Parent, Child, Author, Book, Profile,
ProfileCollection, ParentModelWithCustomPk, ChildModel1, ChildModel2, 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',)) @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/') response = self.client.get('/admin/admin_inlines/capofamiglia/add/')
self.assertContains(response, '<img src="/static/admin/img/icon-unknown.gif" class="help help-tooltip" width="10" height="10" alt="(Help text for ReadOnlyInline)" title="Help text for ReadOnlyInline" />', 1) self.assertContains(response, '<img src="/static/admin/img/icon-unknown.gif" class="help help-tooltip" width="10" height="10" alt="(Help text for ReadOnlyInline)" title="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, '<td class="field-position">')
self.assertContains(response, (
'<input id="id_somechildmodel_set-1-position" '
'name="somechildmodel_set-1-position" type="hidden" value="1" />'))
def test_non_related_name_inline(self): def test_non_related_name_inline(self):
""" """