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 , 6 years ago
| Component: | Uncategorized → contrib.admin |
|---|---|
| Easy pickings: | set |
| Summary: | Extra field of ModelForm without label, raises error in InlineModelAdmin → ModelForm with field without label crashes when used in InlineModelAdmin. |
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Bug |
| Version: | 2.2 → master |
comment:2 by , 6 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
Note:
See TracTickets
for help on using tickets.
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.