From 99512d3544cd792e14927c24a8f92389c4006122 Mon Sep 17 00:00:00 2001 From: Julien Phalip Date: Thu, 13 Oct 2011 13:38:38 +0000 Subject: [PATCH] Fixed #16918 -- Ensured that custom querysets are used when provided to `BaseDateDetailView.get_object()`. Thanks to mitar for the report, to koenb for the patch and to Preston Holmes for the review. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16974 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/views/generic/dates.py | 3 ++- tests/regressiontests/generic_views/dates.py | 16 ++++++++++++++++ tests/regressiontests/generic_views/urls.py | 3 +++ tests/regressiontests/generic_views/views.py | 5 +++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/django/views/generic/dates.py b/django/views/generic/dates.py index c10db3004d..a8391d956e 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 f86e1cd5be..a0dd867e71 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 0f725d8fde..c5c63eec25 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 0c8fd49ff6..00d00219a4 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))