diff options
author | Loek van Gent <loek@1procentclub.nl> | 2015-03-13 11:08:03 +0100 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2015-06-04 21:06:26 -0400 |
commit | 0207bdd2d4157c542c981264c86706b78ca246e9 (patch) | |
tree | 3664db1ac08dda620077c14cfbb1c6da548b0529 /tests/admin_changelist | |
parent | 40f0a84cb151669313faadf857aaddd18d39aaeb (diff) | |
download | django-0207bdd2d4157c542c981264c86706b78ca246e9.tar.gz |
Fixed #24474 -- Allowed configuring the admin's empty change list value.
Diffstat (limited to 'tests/admin_changelist')
-rw-r--r-- | tests/admin_changelist/admin.py | 9 | ||||
-rw-r--r-- | tests/admin_changelist/tests.py | 74 |
2 files changed, 74 insertions, 9 deletions
diff --git a/tests/admin_changelist/admin.py b/tests/admin_changelist/admin.py index 99deafcc62..c32082f78a 100644 --- a/tests/admin_changelist/admin.py +++ b/tests/admin_changelist/admin.py @@ -130,3 +130,12 @@ class DynamicSearchFieldsChildAdmin(admin.ModelAdmin): search_fields = super(DynamicSearchFieldsChildAdmin, self).get_search_fields(request) search_fields += ('age',) return search_fields + + +class EmptyValueChildAdmin(admin.ModelAdmin): + empty_value_display = '-empty-' + list_display = ('name', 'age_display', 'age') + + def age_display(self, obj): + return obj.age + age_display.empty_value_display = '†' diff --git a/tests/admin_changelist/tests.py b/tests/admin_changelist/tests.py index ded3bfc154..86682581b9 100644 --- a/tests/admin_changelist/tests.py +++ b/tests/admin_changelist/tests.py @@ -20,9 +20,9 @@ from .admin import ( BandAdmin, ChildAdmin, ChordsBandAdmin, ConcertAdmin, CustomPaginationAdmin, CustomPaginator, DynamicListDisplayChildAdmin, DynamicListDisplayLinksChildAdmin, DynamicListFilterChildAdmin, - DynamicSearchFieldsChildAdmin, FilteredChildAdmin, GroupAdmin, - InvitationAdmin, NoListDisplayLinksParentAdmin, ParentAdmin, QuartetAdmin, - SwallowAdmin, site as custom_site, + DynamicSearchFieldsChildAdmin, EmptyValueChildAdmin, FilteredChildAdmin, + GroupAdmin, InvitationAdmin, NoListDisplayLinksParentAdmin, ParentAdmin, + QuartetAdmin, SwallowAdmin, site as custom_site, ) from .models import ( Band, Child, ChordsBand, ChordsMusician, Concert, CustomIdUser, Event, @@ -109,14 +109,67 @@ class ChangeListTests(TestCase): list_display = m.get_list_display(request) list_display_links = m.get_list_display_links(request, list_display) cl = ChangeList(request, Child, list_display, list_display_links, - m.list_filter, m.date_hierarchy, m.search_fields, - m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) + m.list_filter, m.date_hierarchy, m.search_fields, + m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) + cl.formset = None + template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') + context = Context({'cl': cl}) + table_output = template.render(context) + link = reverse('admin:admin_changelist_child_change', args=(new_child.id,)) + row_html = ( + '<tbody><tr class="row1"><th class="field-name"><a href="%s">name</a></th>' + '<td class="field-parent nowrap">-</td></tr></tbody>' % link + ) + self.assertNotEqual(table_output.find(row_html), -1, + 'Failed to find expected row element: %s' % table_output) + + def test_result_list_set_empty_value_display_on_admin_site(self): + """ + Test that empty value display can be set on AdminSite + """ + new_child = Child.objects.create(name='name', parent=None) + request = self.factory.get('/child/') + # Set a new empty display value on AdminSite. + admin.site.empty_value_display = '???' + m = ChildAdmin(Child, admin.site) + list_display = m.get_list_display(request) + list_display_links = m.get_list_display_links(request, list_display) + cl = ChangeList(request, Child, list_display, list_display_links, + m.list_filter, m.date_hierarchy, m.search_fields, + m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) + cl.formset = None + template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') + context = Context({'cl': cl}) + table_output = template.render(context) + link = reverse('admin:admin_changelist_child_change', args=(new_child.id,)) + row_html = ( + '<tbody><tr class="row1"><th class="field-name"><a href="%s">name</a></th>' + '<td class="field-parent nowrap">???</td></tr></tbody>' % link + ) + self.assertNotEqual(table_output.find(row_html), -1, + 'Failed to find expected row element: %s' % table_output) + + def test_result_list_set_empty_value_display_in_model_admin(self): + """ + Test that empty value display can be set in ModelAdmin or individual fields. + """ + new_child = Child.objects.create(name='name', parent=None) + request = self.factory.get('/child/') + m = EmptyValueChildAdmin(Child, admin.site) + list_display = m.get_list_display(request) + list_display_links = m.get_list_display_links(request, list_display) + cl = ChangeList(request, Child, list_display, list_display_links, + m.list_filter, m.date_hierarchy, m.search_fields, + m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) cl.formset = None template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') context = Context({'cl': cl}) table_output = template.render(context) link = reverse('admin:admin_changelist_child_change', args=(new_child.id,)) - row_html = '<tbody><tr class="row1"><th class="field-name"><a href="%s">name</a></th><td class="field-parent nowrap">-</td></tr></tbody>' % link + row_html = ( + '<tbody><tr class="row1"><th class="field-name"><a href="%s">name</a></th>' + '<td class="field-age_display">†</td><td class="field-age">-empty-</td></tr></tbody>' % link + ) self.assertNotEqual(table_output.find(row_html), -1, 'Failed to find expected row element: %s' % table_output) @@ -132,14 +185,17 @@ class ChangeListTests(TestCase): list_display = m.get_list_display(request) list_display_links = m.get_list_display_links(request, list_display) cl = ChangeList(request, Child, list_display, list_display_links, - m.list_filter, m.date_hierarchy, m.search_fields, - m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) + m.list_filter, m.date_hierarchy, m.search_fields, + m.list_select_related, m.list_per_page, m.list_max_show_all, m.list_editable, m) cl.formset = None template = Template('{% load admin_list %}{% spaceless %}{% result_list cl %}{% endspaceless %}') context = Context({'cl': cl}) table_output = template.render(context) link = reverse('admin:admin_changelist_child_change', args=(new_child.id,)) - row_html = '<tbody><tr class="row1"><th class="field-name"><a href="%s">name</a></th><td class="field-parent nowrap">Parent object</td></tr></tbody>' % link + row_html = ( + '<tbody><tr class="row1"><th class="field-name"><a href="%s">name</a></th>' + '<td class="field-parent nowrap">Parent object</td></tr></tbody>' % link + ) self.assertNotEqual(table_output.find(row_html), -1, 'Failed to find expected row element: %s' % table_output) |