Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 99512d3544cd792e14927c24a8f92389c4006122 1 parent 43920cd
Julien Phalip authored
3  django/views/generic/dates.py
@@ -459,7 +459,8 @@ def get_object(self, queryset=None):
459 459
                                  month, self.get_month_format(),
460 460
                                  day, self.get_day_format())
461 461
 
462  
-        qs = self.get_queryset()
  462
+        # Use a custom queryset if provided
  463
+        qs = queryset or self.get_queryset()
463 464
 
464 465
         if not self.get_allow_future() and date > datetime.date.today():
465 466
             raise Http404(_(u"Future %(verbose_name_plural)s not available because %(class_name)s.allow_future is False.") % {
16  tests/regressiontests/generic_views/dates.py
@@ -412,3 +412,19 @@ def test_date_detail_allow_future(self):
412 412
     def test_invalid_url(self):
413 413
         self.assertRaises(AttributeError, self.client.get, "/dates/books/2008/oct/01/nopk/")
414 414
 
  415
+    def test_get_object_custom_queryset(self):
  416
+        """
  417
+        Ensure that custom querysets are used when provided to
  418
+        BaseDateDetailView.get_object()
  419
+        Refs #16918.
  420
+        """
  421
+        res = self.client.get(
  422
+            '/dates/books/get_object_custom_queryset/2006/may/01/2/')
  423
+        self.assertEqual(res.status_code, 200)
  424
+        self.assertEqual(res.context['object'], Book.objects.get(pk=2))
  425
+        self.assertEqual(res.context['book'], Book.objects.get(pk=2))
  426
+        self.assertTemplateUsed(res, 'generic_views/book_detail.html')
  427
+
  428
+        res = self.client.get(
  429
+            '/dates/books/get_object_custom_queryset/2008/oct/01/1/')
  430
+        self.assertEqual(res.status_code, 404)
3  tests/regressiontests/generic_views/urls.py
@@ -216,6 +216,9 @@
216 216
     (r'^dates/books/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{1,2})/byslug/(?P<slug>[\w-]+)/$',
217 217
         views.BookDetail.as_view()),
218 218
 
  219
+    (r'^dates/books/get_object_custom_queryset/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{1,2})/(?P<pk>\d+)/$',
  220
+        views.BookDetailGetObjectCustomQueryset.as_view()),
  221
+
219 222
     # Useful for testing redirects
220 223
     (r'^accounts/login/$',  'django.contrib.auth.views.login')
221 224
 )
5  tests/regressiontests/generic_views/views.py
@@ -177,3 +177,8 @@ class BookDetail(BookConfig, generic.DateDetailView):
177 177
 class AuthorGetQuerySetFormView(generic.edit.ModelFormMixin):
178 178
     def get_queryset(self):
179 179
         return Author.objects.all()
  180
+
  181
+class BookDetailGetObjectCustomQueryset(BookDetail):
  182
+    def get_object(self, queryset=None):
  183
+        return super(BookDetailGetObjectCustomQueryset,self).get_object(
  184
+            queryset=Book.objects.filter(pk=2))

0 notes on commit 99512d3

Please sign in to comment.
Something went wrong with that request. Please try again.