Skip to content

Commit

Permalink
[2.1.x] Fixed #29682 -- Fixed admin change form crash if a view-only …
Browse files Browse the repository at this point in the history
…model's form has an extra field.

Backport of d311124 from master
  • Loading branch information
timgraham committed Aug 20, 2018
1 parent 77498db commit 53b9a66
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
2 changes: 1 addition & 1 deletion django/contrib/admin/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def __init__(self, form, field, is_first, model_admin=None):
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]
Expand Down
6 changes: 5 additions & 1 deletion django/contrib/admin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"):
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/2.1.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ Bugfixes

* Made the admin change view redirect to the changelist view after a POST if
the user has the 'view' permission (:ticket:`29663`).

* Fixed admin change view crash for view-only users if the form has an extra
form field (:ticket:`29682`).
16 changes: 16 additions & 0 deletions tests/admin_utils/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,22 @@ def test_from_model(self, obj):
("not Really the Model", MockModelAdmin.test_from_model)
)

def test_label_for_field_form_argument(self):
class ArticleForm(forms.ModelForm):
extra_form_field = forms.BooleanField()

class Meta:
fields = '__all__'
model = Article

self.assertEqual(
label_for_field('extra_form_field', Article, form=ArticleForm()),
'Extra form field'
)
msg = "Unable to lookup 'nonexistent' on Article or ArticleForm"
with self.assertRaisesMessage(AttributeError, msg):
label_for_field('nonexistent', Article, form=ArticleForm()),

def test_label_for_property(self):
# NOTE: cannot use @property decorator, because of
# AttributeError: 'property' object has no attribute 'short_description'
Expand Down
11 changes: 10 additions & 1 deletion tests/admin_views/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ class ChapterXtra1Admin(admin.ModelAdmin):
)


class ArticleForm(forms.ModelForm):
extra_form_field = forms.BooleanField(required=False)

class Meta:
fields = '__all__'
model = Article


class ArticleAdmin(admin.ModelAdmin):
list_display = (
'content', 'date', callable_year, 'model_year', 'modeladmin_year',
Expand All @@ -101,10 +109,11 @@ class ArticleAdmin(admin.ModelAdmin):
list_filter = ('date', 'section')
autocomplete_fields = ('section',)
view_on_site = False
form = ArticleForm
fieldsets = (
('Some fields', {
'classes': ('collapse',),
'fields': ('title', 'content')
'fields': ('title', 'content', 'extra_form_field'),
}),
('Some other fields', {
'classes': ('wide',),
Expand Down
1 change: 1 addition & 0 deletions tests/admin_views/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,7 @@ def test_change_view(self):
response = self.client.get(article_change_url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['title'], 'View article')
self.assertContains(response, '<label>Extra form field:</label>')
self.assertContains(response, '<a href="/test_admin/admin/admin_views/article/" class="closelink">Close</a>')
post = self.client.post(article_change_url, change_dict)
self.assertEqual(post.status_code, 302)
Expand Down

0 comments on commit 53b9a66

Please sign in to comment.