0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Merge pull request #5147 from leewesleyv/feature/4320-extend-inspect-field-labels

Reuse the label_for_field function for fields in the InspectView
This commit is contained in:
Andy Babic 2019-04-10 21:09:01 +01:00 committed by GitHub
commit 5c336f223e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 11 deletions

View File

@ -103,7 +103,7 @@ class TestCreateView(TestCase, WagtailTestUtils):
class TestInspectView(TestCase, WagtailTestUtils):
fixtures = ['test_specific.json']
fixtures = ['test_specific.json', 'modeladmintest_test.json']
def setUp(self):
self.login()
@ -157,6 +157,16 @@ class TestInspectView(TestCase, WagtailTestUtils):
response = self.get(100)
self.assertEqual(response.status_code, 404)
def test_short_description_is_used_as_field_label(self):
"""
A custom field has been added to the inspect view's `inspect_view_fields` and since
this field has a `short_description` we expect it to be used as the field's label,
and not use the name of the function.
"""
response = self.client.get('/admin/modeladmintest/author/inspect/1/')
self.assertContains(response, 'Birth information')
self.assertNotContains(response, 'author_birth_string')
class TestEditView(TestCase, WagtailTestUtils):
fixtures = ['test_specific.json']

View File

@ -7,7 +7,7 @@ from django.contrib.admin import FieldListFilter, widgets
from django.contrib.admin.exceptions import DisallowedModelAdminLookup
from django.contrib.admin.options import IncorrectLookupParameters
from django.contrib.admin.utils import (
get_fields_from_path, lookup_needs_distinct, prepare_lookup_value, quote, unquote)
get_fields_from_path, label_for_field, lookup_needs_distinct, prepare_lookup_value, quote, unquote)
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ImproperlyConfigured, PermissionDenied, SuspiciousOperation
from django.core.paginator import InvalidPage, Paginator
@ -844,14 +844,7 @@ class InspectView(InstanceSpecificView):
def get_field_label(self, field_name, field=None):
""" Return a label to display for a field """
label = None
if field is not None:
label = getattr(field, 'verbose_name', None)
if label is None:
label = getattr(field, 'name', None)
if label is None:
label = field_name
return label
return label_for_field(field_name, model=self.model)
def get_field_display_value(self, field_name, field=None):
""" Return a display value for a field/attribute """

View File

@ -9,6 +9,11 @@ class Author(models.Model):
name = models.CharField(max_length=255)
date_of_birth = models.DateField()
def author_birth_string(self):
return 'This author was born in pallet town'
author_birth_string.short_description = "Birth information"
def __str__(self):
return self.name

View File

@ -15,7 +15,7 @@ class AuthorModelAdmin(ModelAdmin):
list_filter = ('date_of_birth', )
search_fields = ('name', )
inspect_view_enabled = True
inspect_view_fields = ('name', )
inspect_view_fields = ('name', 'author_birth_string')
def last_book(self, obj):
# For testing use of modeladmin methods in list_display