Fixed #31590 -- Fixed ModelAdmin.date_hierarchy crash with an empty QuerySet.

Regression in 55cdf6c52d.
This commit is contained in:
kjpc-tech 2020-05-14 16:56:21 -06:00 committed by Mariusz Felisiak
parent 9d211f149a
commit 099bce1bf0
2 changed files with 10 additions and 5 deletions

View File

@ -380,12 +380,12 @@ def date_hierarchy(cl):
# select appropriate start level # select appropriate start level
date_range = cl.queryset.aggregate(first=models.Min(field_name), date_range = cl.queryset.aggregate(first=models.Min(field_name),
last=models.Max(field_name)) last=models.Max(field_name))
if date_range['first'] and date_range['last']:
if dates_or_datetimes == 'datetimes': if dates_or_datetimes == 'datetimes':
date_range = { date_range = {
k: timezone.localtime(v) if timezone.is_aware(v) else v k: timezone.localtime(v) if timezone.is_aware(v) else v
for k, v in date_range.items() for k, v in date_range.items()
} }
if date_range['first'] and date_range['last']:
if date_range['first'].year == date_range['last'].year: if date_range['first'].year == date_range['last'].year:
year_lookup = date_range['first'].year year_lookup = date_range['first'].year
if date_range['first'].month == date_range['last'].month: if date_range['first'].month == date_range['last'].month:

View File

@ -969,6 +969,11 @@ class AdminViewBasicTest(AdminViewBasicTestCase):
self.assertEqual(response.context['site_url'], '/my-site-url/') self.assertEqual(response.context['site_url'], '/my-site-url/')
self.assertContains(response, '<a href="/my-site-url/">View site</a>') self.assertContains(response, '<a href="/my-site-url/">View site</a>')
def test_date_hierarchy_empty_queryset(self):
self.assertIs(Question.objects.exists(), False)
response = self.client.get(reverse('admin:admin_views_answer2_changelist'))
self.assertEqual(response.status_code, 200)
@override_settings(TIME_ZONE='America/Sao_Paulo', USE_TZ=True) @override_settings(TIME_ZONE='America/Sao_Paulo', USE_TZ=True)
def test_date_hierarchy_timezone_dst(self): def test_date_hierarchy_timezone_dst(self):
# This datetime doesn't exist in this timezone due to DST. # This datetime doesn't exist in this timezone due to DST.