models.py:
{{{#!python
class Parent(models.Model):
pass
class Child(models.Model):
parent = models.ForeignKey(Parent, on_delete=models.PROTECT)
}}}
forms.py:
{{{#!python
class ChildForm(forms.ModelForm):
extra_field = forms.CharField()
class Meta:
model = Child
fields = '__all__'
}}}
admin.py:
{{{#!python
@admin.register(models.Child)
class ChildAdmin(admin.ModelAdmin):
'''The ModelForm renders as expected'''
form = forms.ChildForm
class ChildInline(admin.TabularInline):
'''Here the ModelForm without a label in the extra field, will raise
error'''
model = models.Child
form = forms.ChildForm
@admin.register(models.Parent)
class ParentAdmin(admin.ModelAdmin):
inlines = (ChildInline,)
}}}
{{{#!python
File "/home/venv/lined/lib/python3.7/site-
packages/django/contrib/admin/utils.py", line 364, in label_for_field
raise AttributeError(message)
AttributeError: Unable to lookup 'extra_field' on Child or
ChildInline
}}}
Full error message:
https://pastebin.com/89MUGchf
--
Ticket URL: <https://code.djangoproject.com/ticket/30520>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* component: Uncategorized => contrib.admin
* version: 2.2 => master
* easy: 0 => 1
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted
Comment:
Thanks for the report. I think that passing an empty form to the
`label_for_field()` should fix this issue, e.g.
{{{
diff --git a/django/contrib/admin/helpers.py
b/django/contrib/admin/helpers.py
index 83719f4346..aa07581391 100644
--- a/django/contrib/admin/helpers.py
+++ b/django/contrib/admin/helpers.py
@@ -289,7 +289,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,
}}}
Reproduced at 67b6cb7723b2765cb776bd59d5603e3e63eefc2e.
--
Ticket URL: <https://code.djangoproject.com/ticket/30520#comment:1>
* status: new => assigned
* owner: nobody => Jones
--
Ticket URL: <https://code.djangoproject.com/ticket/30520#comment:2>
Comment (by Jones):
[https://github.com/django/django/pull/11426 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/30520#comment:3>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/30520#comment:4>
* stage: Ready for checkin => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/30520#comment:5>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/11427 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/30520#comment:4>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"f9561144d79bad88ee5781e3b2c09fb7c8f7fd7d" f9561144]:
{{{
#!CommitTicketReference repository=""
revision="f9561144d79bad88ee5781e3b2c09fb7c8f7fd7d"
Fixed #30520 -- Fixed crash of admin model inlines on custom fields
without labels.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30520#comment:5>