diff options
| author | Tim Graham <timograham@gmail.com> | 2018-08-18 16:15:18 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-08-20 11:14:20 -0400 |
| commit | d311124be59df64278f3149d68e79ce45b8a6c64 (patch) | |
| tree | 1c47886fa1d9d210723957f12ea541003e2f2a30 /django/contrib/admin | |
| parent | 0e7a9525baec11d75badc37f8d8b92f17dba60ae (diff) | |
Fixed #29682 -- Fixed admin change form crash if a view-only model's form has an extra field.
Diffstat (limited to 'django/contrib/admin')
| -rw-r--r-- | django/contrib/admin/helpers.py | 2 | ||||
| -rw-r--r-- | django/contrib/admin/utils.py | 6 |
2 files changed, 6 insertions, 2 deletions
diff --git a/django/contrib/admin/helpers.py b/django/contrib/admin/helpers.py index d0350c3930..6fb35be1f3 100644 --- a/django/contrib/admin/helpers.py +++ b/django/contrib/admin/helpers.py @@ -162,7 +162,7 @@ class AdminReadonlyField: if form._meta.labels and class_name in form._meta.labels: label = form._meta.labels[class_name] else: - label = label_for_field(field, form._meta.model, model_admin) + label = label_for_field(field, form._meta.model, model_admin, form=form) if form._meta.help_texts and class_name in form._meta.help_texts: help_text = form._meta.help_texts[class_name] diff --git a/django/contrib/admin/utils.py b/django/contrib/admin/utils.py index eae09b2238..1db552bcd8 100644 --- a/django/contrib/admin/utils.py +++ b/django/contrib/admin/utils.py @@ -319,7 +319,7 @@ def _get_non_gfk_field(opts, name): return field -def label_for_field(name, model, model_admin=None, return_attr=False): +def label_for_field(name, model, model_admin=None, return_attr=False, form=None): """ Return a sensible label for a field name. The name can be a callable, property (but not created with @property decorator), or the name of an @@ -346,10 +346,14 @@ def label_for_field(name, model, model_admin=None, return_attr=False): attr = getattr(model_admin, name) elif hasattr(model, name): attr = getattr(model, name) + elif form and name in form.fields: + attr = form.fields[name] else: message = "Unable to lookup '%s' on %s" % (name, model._meta.object_name) if model_admin: message += " or %s" % (model_admin.__class__.__name__,) + if form: + message += " or %s" % form.__class__.__name__ raise AttributeError(message) if hasattr(attr, "short_description"): |
