From 118b11221f7f632b4d0e6e976c87f563746ec211 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Tue, 3 Feb 2015 08:46:17 -0500 Subject: [PATCH] Fixed #24263 -- Prevented extra queries on BaseDateDetailView with a custom queryset. Thanks jekka-ua for the report and patch. --- django/views/generic/dates.py | 2 +- tests/generic_views/test_dates.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/django/views/generic/dates.py b/django/views/generic/dates.py index 8df5c338e2f..27f0ba10652 100644 --- a/django/views/generic/dates.py +++ b/django/views/generic/dates.py @@ -648,7 +648,7 @@ class BaseDateDetailView(YearMixin, MonthMixin, DayMixin, DateMixin, BaseDetailV day, self.get_day_format()) # Use a custom queryset if provided - qs = queryset or self.get_queryset() + qs = self.get_queryset() if queryset is None else queryset if not self.get_allow_future() and date > datetime.date.today(): raise Http404(_( diff --git a/tests/generic_views/test_dates.py b/tests/generic_views/test_dates.py index cbd9bd90a03..d354d421498 100644 --- a/tests/generic_views/test_dates.py +++ b/tests/generic_views/test_dates.py @@ -638,6 +638,10 @@ class DateDetailViewTests(TestCase): '/dates/books/get_object_custom_queryset/2008/oct/01/1/') self.assertEqual(res.status_code, 404) + def test_get_object_custom_queryset_numqueries(self): + with self.assertNumQueries(1): + self.client.get('/dates/books/get_object_custom_queryset/2006/may/01/2/') + def test_datetime_date_detail(self): bs = BookSigning.objects.create(event_date=datetime.datetime(2008, 4, 2, 12, 0)) res = self.client.get('/dates/booksignings/2008/apr/2/%d/' % bs.pk)