Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@jphalip jphalip authored
View
3  django/views/generic/dates.py
@@ -459,7 +459,8 @@ def get_object(self, queryset=None):
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.") % {
View
16 tests/regressiontests/generic_views/dates.py
@@ -412,3 +412,19 @@ def test_date_detail_allow_future(self):
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)
View
3  tests/regressiontests/generic_views/urls.py
@@ -216,6 +216,9 @@
(r'^dates/books/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{1,2})/byslug/(?P<slug>[\w-]+)/$',
views.BookDetail.as_view()),
+ (r'^dates/books/get_object_custom_queryset/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{1,2})/(?P<pk>\d+)/$',
+ views.BookDetailGetObjectCustomQueryset.as_view()),
+
# Useful for testing redirects
(r'^accounts/login/$', 'django.contrib.auth.views.login')
)
View
5 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))
Please sign in to comment.
Something went wrong with that request. Please try again.