From f9561144d79bad88ee5781e3b2c09fb7c8f7fd7d Mon Sep 17 00:00:00 2001 From: Jones Ambrosi Date: Tue, 28 May 2019 14:57:31 -0300 Subject: [PATCH] Fixed #30520 -- Fixed crash of admin model inlines on custom fields without labels. --- django/contrib/admin/helpers.py | 9 ++++++-- tests/admin_inlines/admin.py | 37 ++++++++++++++++++++++++++++++--- tests/admin_inlines/tests.py | 10 +++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/django/contrib/admin/helpers.py b/django/contrib/admin/helpers.py index 83719f4346..0337c50010 100644 --- a/django/contrib/admin/helpers.py +++ b/django/contrib/admin/helpers.py @@ -280,7 +280,12 @@ class InlineAdminFormSet: if not self.has_change_permission or field_name in self.readonly_fields: yield { 'name': field_name, - 'label': meta_labels.get(field_name) or label_for_field(field_name, self.opts.model, self.opts), + 'label': meta_labels.get(field_name) or label_for_field( + field_name, + self.opts.model, + self.opts, + form=empty_form, + ), 'widget': {'is_hidden': False}, 'required': False, 'help_text': meta_help_texts.get(field_name) or help_text_for_field(field_name, self.opts.model), @@ -289,7 +294,7 @@ class InlineAdminFormSet: form_field = empty_form.fields[field_name] label = form_field.label if label is None: - label = label_for_field(field_name, self.opts.model, self.opts) + label = label_for_field(field_name, self.opts.model, self.opts, form=empty_form) yield { 'name': field_name, 'label': label, diff --git a/tests/admin_inlines/admin.py b/tests/admin_inlines/admin.py index dd2624943e..f4071efa4e 100644 --- a/tests/admin_inlines/admin.py +++ b/tests/admin_inlines/admin.py @@ -4,10 +4,10 @@ from django.db import models from .models import ( Author, BinaryTree, CapoFamiglia, Chapter, Child, ChildModel1, ChildModel2, - Consigliere, EditablePKBook, ExtraTerrestrial, Fashionista, Holder, - Holder2, Holder3, Holder4, Inner, Inner2, Inner3, Inner4Stacked, + Consigliere, EditablePKBook, ExtraTerrestrial, Fashionista, FootNote, + Holder, Holder2, Holder3, Holder4, Inner, Inner2, Inner3, Inner4Stacked, Inner4Tabular, NonAutoPKBook, NonAutoPKBookChild, Novel, - NovelReadonlyChapter, ParentModelWithCustomPk, Poll, Profile, + NovelReadonlyChapter, OutfitItem, ParentModelWithCustomPk, Poll, Profile, ProfileCollection, Question, ReadOnlyInline, ShoppingWeakness, Sighting, SomeChildModel, SomeParentModel, SottoCapo, Teacher, Title, TitleCollection, @@ -131,6 +131,35 @@ class InlineWeakness(admin.TabularInline): extra = 1 +class WeaknessForm(forms.ModelForm): + extra_field = forms.CharField() + + class Meta: + model = ShoppingWeakness + fields = '__all__' + + +class WeaknessInlineCustomForm(admin.TabularInline): + model = ShoppingWeakness + form = WeaknessForm + + +class FootNoteForm(forms.ModelForm): + extra_field = forms.CharField() + + class Meta: + model = FootNote + fields = '__all__' + + +class FootNoteNonEditableInlineCustomForm(admin.TabularInline): + model = FootNote + form = FootNoteForm + + def has_change_permission(self, request, obj=None): + return False + + class QuestionInline(admin.TabularInline): model = Question readonly_fields = ['call_me'] @@ -271,3 +300,5 @@ site.register(ExtraTerrestrial, inlines=[SightingInline]) site.register(SomeParentModel, inlines=[SomeChildModelInline]) site.register([Question, Inner4Stacked, Inner4Tabular]) site.register(Teacher, TeacherAdmin) +site.register(Chapter, inlines=[FootNoteNonEditableInlineCustomForm]) +site.register(OutfitItem, inlines=[WeaknessInlineCustomForm]) diff --git a/tests/admin_inlines/tests.py b/tests/admin_inlines/tests.py index 09fcd0f300..fe0d913b0d 100644 --- a/tests/admin_inlines/tests.py +++ b/tests/admin_inlines/tests.py @@ -108,6 +108,16 @@ class TestInline(TestDataMixin, TestCase): response = self.client.get(reverse('admin:admin_inlines_titlecollection_add')) self.assertContains(response, 'Title1', html=True) + def test_custom_form_tabular_inline_extra_field_label(self): + response = self.client.get(reverse('admin:admin_inlines_outfititem_add')) + _, extra_field = list(response.context['inline_admin_formset'].fields()) + self.assertEqual(extra_field['label'], 'Extra field') + + def test_non_editable_custom_form_tabular_inline_extra_field_label(self): + response = self.client.get(reverse('admin:admin_inlines_chapter_add')) + _, extra_field = list(response.context['inline_admin_formset'].fields()) + self.assertEqual(extra_field['label'], 'Extra field') + def test_custom_form_tabular_inline_overridden_label(self): """ SomeChildModelForm.__init__() overrides the label of a form field.