Opened 6 years ago

Closed 6 years ago

#30520 closed Bug (fixed)

ModelForm with field without label crashes when used in InlineModelAdmin.

Reported by: George Tantiras Owned by: Jones
Component: contrib.admin Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

As per this and 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:

class Parent(models.Model):
    pass


class Child(models.Model):
    parent = models.ForeignKey(Parent, on_delete=models.PROTECT)

forms.py:

class ChildForm(forms.ModelForm):
    extra_field = forms.CharField()

    class Meta:
        model = Child
        fields = '__all__'

admin.py:

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

Change History (5)

comment:1 by Mariusz Felisiak, 6 years ago

Component: Uncategorizedcontrib.admin
Easy pickings: set
Summary: Extra field of ModelForm without label, raises error in InlineModelAdminModelForm with field without label crashes when used in InlineModelAdmin.
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug
Version: 2.2master

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.

comment:2 by Jones, 6 years ago

Owner: changed from nobody to Jones
Status: newassigned

comment:3 by Jones, 6 years ago

Last edited 6 years ago by Jones (previous) (diff)

comment:4 by Mariusz Felisiak, 6 years ago

Has patch: set
Version 0, edited 6 years ago by Mariusz Felisiak (next)

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In f9561144:

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

Note: See TracTickets for help on using tickets.
Back to Top