[Django] #30520: Extra field of ModelForm without label, raises error in InlineModelAdmin

12 views
Skip to first unread message

Django

unread,
May 28, 2019, 6:53:13 AM5/28/19
to django-...@googlegroups.com
#30520: Extra field of ModelForm without label, raises error in InlineModelAdmin
-------------------------------------------+------------------------
Reporter: George Tantiras | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
As per [https://stackoverflow.com/questions/56156039/extra-field-of-
modelform-without-label-does-not-render-in-inlinemodeladmin this] and
[https://stackoverflow.com/questions/54511865/django-extra-fields-in-
custommodelform-is-giving-unable-to-lookuup-error-i that] S.O. issues,
although a ModelForm with an extra field which has no label works at the
model's change view, it raises an error when the same ModelForm is used
for an inline:

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.

Django

unread,
May 28, 2019, 9:27:56 AM5/28/19
to django-...@googlegroups.com
#30520: ModelForm with field without label crashes when used in InlineModelAdmin.
---------------------------------+------------------------------------

Reporter: George Tantiras | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by felixxm):

* 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>

Django

unread,
May 28, 2019, 10:04:58 AM5/28/19
to django-...@googlegroups.com
#30520: ModelForm with field without label crashes when used in InlineModelAdmin.
---------------------------------+------------------------------------
Reporter: George Tantiras | Owner: Jones
Type: Bug | Status: assigned

Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Jones):

* status: new => assigned
* owner: nobody => Jones


--
Ticket URL: <https://code.djangoproject.com/ticket/30520#comment:2>

Django

unread,
May 28, 2019, 11:39:35 AM5/28/19
to django-...@googlegroups.com
#30520: ModelForm with field without label crashes when used in InlineModelAdmin.
---------------------------------+------------------------------------
Reporter: George Tantiras | Owner: Jones
Type: Bug | Status: assigned
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------

Comment (by Jones):

[https://github.com/django/django/pull/11426 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/30520#comment:3>

Django

unread,
May 28, 2019, 12:45:06 PM5/28/19
to django-...@googlegroups.com
#30520: ModelForm with field without label crashes when used in InlineModelAdmin.
-------------------------------------+-------------------------------------

Reporter: George Tantiras | Owner: Jones
Type: Bug | Status: assigned
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jones):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/30520#comment:4>

Django

unread,
May 28, 2019, 12:55:49 PM5/28/19
to django-...@googlegroups.com
#30520: ModelForm with field without label crashes when used in InlineModelAdmin.
---------------------------------+------------------------------------

Reporter: George Tantiras | Owner: Jones
Type: Bug | Status: assigned
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Jones):

* stage: Ready for checkin => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/30520#comment:5>

Django

unread,
May 29, 2019, 6:39:10 AM5/29/19
to django-...@googlegroups.com
#30520: ModelForm with field without label crashes when used in InlineModelAdmin.
---------------------------------+------------------------------------
Reporter: George Tantiras | Owner: Jones
Type: Bug | Status: assigned
Component: contrib.admin | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by felixxm):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/11427 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/30520#comment:4>

Django

unread,
Jun 4, 2019, 5:02:57 AM6/4/19
to django-...@googlegroups.com
#30520: ModelForm with field without label crashes when used in InlineModelAdmin.
---------------------------------+------------------------------------
Reporter: George Tantiras | Owner: Jones
Type: Bug | Status: closed
Component: contrib.admin | Version: master
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* 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>

Reply all
Reply to author
Forward
0 new messages