Fixed #30520 -- Fixed crash of admin model inlines on custom fields without labels.

This commit is contained in:
Jones Ambrosi 2019-05-28 14:57:31 -03:00 committed by Mariusz Felisiak
parent aa94f7c899
commit f9561144d7
3 changed files with 51 additions and 5 deletions

View File

@ -280,7 +280,12 @@ class InlineAdminFormSet:
if not self.has_change_permission or field_name in self.readonly_fields: if not self.has_change_permission or field_name in self.readonly_fields:
yield { yield {
'name': field_name, '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}, 'widget': {'is_hidden': False},
'required': False, 'required': False,
'help_text': meta_help_texts.get(field_name) or help_text_for_field(field_name, self.opts.model), '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] form_field = empty_form.fields[field_name]
label = form_field.label label = form_field.label
if label is None: 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 { yield {
'name': field_name, 'name': field_name,
'label': label, 'label': label,

View File

@ -4,10 +4,10 @@ from django.db import models
from .models import ( from .models import (
Author, BinaryTree, CapoFamiglia, Chapter, Child, ChildModel1, ChildModel2, Author, BinaryTree, CapoFamiglia, Chapter, Child, ChildModel1, ChildModel2,
Consigliere, EditablePKBook, ExtraTerrestrial, Fashionista, Holder, Consigliere, EditablePKBook, ExtraTerrestrial, Fashionista, FootNote,
Holder2, Holder3, Holder4, Inner, Inner2, Inner3, Inner4Stacked, Holder, Holder2, Holder3, Holder4, Inner, Inner2, Inner3, Inner4Stacked,
Inner4Tabular, NonAutoPKBook, NonAutoPKBookChild, Novel, Inner4Tabular, NonAutoPKBook, NonAutoPKBookChild, Novel,
NovelReadonlyChapter, ParentModelWithCustomPk, Poll, Profile, NovelReadonlyChapter, OutfitItem, ParentModelWithCustomPk, Poll, Profile,
ProfileCollection, Question, ReadOnlyInline, ShoppingWeakness, Sighting, ProfileCollection, Question, ReadOnlyInline, ShoppingWeakness, Sighting,
SomeChildModel, SomeParentModel, SottoCapo, Teacher, Title, SomeChildModel, SomeParentModel, SottoCapo, Teacher, Title,
TitleCollection, TitleCollection,
@ -131,6 +131,35 @@ class InlineWeakness(admin.TabularInline):
extra = 1 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): class QuestionInline(admin.TabularInline):
model = Question model = Question
readonly_fields = ['call_me'] readonly_fields = ['call_me']
@ -271,3 +300,5 @@ site.register(ExtraTerrestrial, inlines=[SightingInline])
site.register(SomeParentModel, inlines=[SomeChildModelInline]) site.register(SomeParentModel, inlines=[SomeChildModelInline])
site.register([Question, Inner4Stacked, Inner4Tabular]) site.register([Question, Inner4Stacked, Inner4Tabular])
site.register(Teacher, TeacherAdmin) site.register(Teacher, TeacherAdmin)
site.register(Chapter, inlines=[FootNoteNonEditableInlineCustomForm])
site.register(OutfitItem, inlines=[WeaknessInlineCustomForm])

View File

@ -108,6 +108,16 @@ class TestInline(TestDataMixin, TestCase):
response = self.client.get(reverse('admin:admin_inlines_titlecollection_add')) response = self.client.get(reverse('admin:admin_inlines_titlecollection_add'))
self.assertContains(response, '<th class="column-title1 required">Title1</th>', html=True) self.assertContains(response, '<th class="column-title1 required">Title1</th>', 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): def test_custom_form_tabular_inline_overridden_label(self):
""" """
SomeChildModelForm.__init__() overrides the label of a form field. SomeChildModelForm.__init__() overrides the label of a form field.