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
This commit is contained in:
Julien Phalip 2011-10-13 13:38:38 +00:00
parent 43920cd32e
commit 99512d3544
4 changed files with 26 additions and 1 deletions

View File

@ -459,7 +459,8 @@ class BaseDateDetailView(YearMixin, MonthMixin, DayMixin, DateMixin, BaseDetailV
month, self.get_month_format(), month, self.get_month_format(),
day, self.get_day_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(): 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.") % { raise Http404(_(u"Future %(verbose_name_plural)s not available because %(class_name)s.allow_future is False.") % {

View File

@ -412,3 +412,19 @@ class DateDetailViewTests(TestCase):
def test_invalid_url(self): def test_invalid_url(self):
self.assertRaises(AttributeError, self.client.get, "/dates/books/2008/oct/01/nopk/") 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 File

@ -216,6 +216,9 @@ urlpatterns = patterns('',
(r'^dates/books/(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\d{1,2})/byslug/(?P<slug>[\w-]+)/$', (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()), 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 # Useful for testing redirects
(r'^accounts/login/$', 'django.contrib.auth.views.login') (r'^accounts/login/$', 'django.contrib.auth.views.login')
) )

View File

@ -177,3 +177,8 @@ class BookDetail(BookConfig, generic.DateDetailView):
class AuthorGetQuerySetFormView(generic.edit.ModelFormMixin): class AuthorGetQuerySetFormView(generic.edit.ModelFormMixin):
def get_queryset(self): def get_queryset(self):
return Author.objects.all() 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))