diff --git a/django/views/generic/dates.py b/django/views/generic/dates.py index c10db3004d6..a8391d956e0 100644 --- a/django/views/generic/dates.py +++ b/django/views/generic/dates.py @@ -459,7 +459,8 @@ class BaseDateDetailView(YearMixin, MonthMixin, DayMixin, DateMixin, BaseDetailV month, self.get_month_format(), day, self.get_day_format()) - qs = self.get_queryset() + # Use a custom queryset if provided + qs = queryset or self.get_queryset() if not self.get_allow_future() and date > datetime.date.today(): raise Http404(_(u"Future %(verbose_name_plural)s not available because %(class_name)s.allow_future is False.") % { diff --git a/tests/regressiontests/generic_views/dates.py b/tests/regressiontests/generic_views/dates.py index f86e1cd5be9..a0dd867e71c 100644 --- a/tests/regressiontests/generic_views/dates.py +++ b/tests/regressiontests/generic_views/dates.py @@ -412,3 +412,19 @@ class DateDetailViewTests(TestCase): def test_invalid_url(self): self.assertRaises(AttributeError, self.client.get, "/dates/books/2008/oct/01/nopk/") + def test_get_object_custom_queryset(self): + """ + Ensure that custom querysets are used when provided to + BaseDateDetailView.get_object() + Refs #16918. + """ + res = self.client.get( + '/dates/books/get_object_custom_queryset/2006/may/01/2/') + self.assertEqual(res.status_code, 200) + self.assertEqual(res.context['object'], Book.objects.get(pk=2)) + self.assertEqual(res.context['book'], Book.objects.get(pk=2)) + self.assertTemplateUsed(res, 'generic_views/book_detail.html') + + res = self.client.get( + '/dates/books/get_object_custom_queryset/2008/oct/01/1/') + self.assertEqual(res.status_code, 404) diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py index 0f725d8fde4..c5c63eec254 100644 --- a/tests/regressiontests/generic_views/urls.py +++ b/tests/regressiontests/generic_views/urls.py @@ -216,6 +216,9 @@ urlpatterns = patterns('', (r'^dates/books/(?P\d{4})/(?P[a-z]{3})/(?P\d{1,2})/byslug/(?P[\w-]+)/$', views.BookDetail.as_view()), + (r'^dates/books/get_object_custom_queryset/(?P\d{4})/(?P[a-z]{3})/(?P\d{1,2})/(?P\d+)/$', + views.BookDetailGetObjectCustomQueryset.as_view()), + # Useful for testing redirects (r'^accounts/login/$', 'django.contrib.auth.views.login') ) diff --git a/tests/regressiontests/generic_views/views.py b/tests/regressiontests/generic_views/views.py index 0c8fd49ff69..00d00219a42 100644 --- a/tests/regressiontests/generic_views/views.py +++ b/tests/regressiontests/generic_views/views.py @@ -177,3 +177,8 @@ class BookDetail(BookConfig, generic.DateDetailView): class AuthorGetQuerySetFormView(generic.edit.ModelFormMixin): def get_queryset(self): return Author.objects.all() + +class BookDetailGetObjectCustomQueryset(BookDetail): + def get_object(self, queryset=None): + return super(BookDetailGetObjectCustomQueryset,self).get_object( + queryset=Book.objects.filter(pk=2))