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:
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,

View File

@ -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])

View File

@ -108,6 +108,16 @@ class TestInline(TestDataMixin, TestCase):
response = self.client.get(reverse('admin:admin_inlines_titlecollection_add'))
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):
"""
SomeChildModelForm.__init__() overrides the label of a form field.