Improve rendering of ManyToManyField values (or any other property / attribute that returns a Manager) in InspectView (#3423)

* Fix for #3298
* Improve rendering of ManyToManyField values (or any other property / attribute that returns a Manager) in InspectView
pull/3531/head
Andy Babic 2017-04-10 19:14:40 +01:00 zatwierdzone przez Matt Westcott
rodzic 9132bcc23d
commit aef2428072
2 zmienionych plików z 35 dodań i 2 usunięć

Wyświetl plik

@ -4,7 +4,7 @@ from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group, Permission
from django.test import TestCase
from wagtail.tests.testapp.models import BusinessIndex
from wagtail.tests.testapp.models import BusinessIndex, EventCategory, EventPage
from wagtail.tests.utils import WagtailTestUtils
from wagtail.wagtailcore.models import GroupPagePermission, Page
@ -125,6 +125,29 @@ class TestInspectView(TestCase, WagtailTestUtils):
response = self.get(4)
self.assertContains(response, 'Christmas', 2)
def test_manytomany_output(self):
"""
Because ManyToMany fields are output InspectView by default, the
`categories` for the event should output as a comma separated list
once populated.
"""
eventpage = EventPage.objects.get(pk=4)
free_category = EventCategory.objects.create(name='Free')
child_friendly_category = EventCategory.objects.create(name='Child-friendly')
eventpage.categories = (free_category, child_friendly_category)
eventpage.save()
response = self.get(4)
self.assertContains(response, '<dd>Free, Child-friendly</dd>', html=True)
def test_false_values_displayed(self):
"""
Boolean fields with False values should display False, rather than the
value of `get_empty_value_display()`. For this page, those should be
`locked`, `expired` and `has_unpublished_changes`
"""
response = self.get(4)
self.assertContains(response, '<dd>False</dd>', count=3, html=True)
def test_location_present(self):
"""
The location should appear once, in the field listing

Wyświetl plik

@ -861,6 +861,14 @@ class InspectView(InstanceSpecificView):
# we can render something useful. raises AttributeError appropriately.
val = getattr(self.instance, field_name)
if isinstance(val, models.Manager):
val = val.all()
if isinstance(val, models.QuerySet):
if val.exists():
return ', '.join(['%s' % obj for obj in val])
return self.model_admin.get_empty_value_display(field_name)
# wagtail.wagtailimages might not be installed
try:
from wagtail.wagtailimages.models import AbstractImage
@ -880,7 +888,9 @@ class InspectView(InstanceSpecificView):
pass
# Resort to returning the real value or 'empty value'
return val or self.model_admin.get_empty_value_display(field_name)
if val or val is False:
return val
return self.model_admin.get_empty_value_display(field_name)
def get_image_field_display(self, field_name, field):
""" Render an image """