Fixed #18087 -- Prevented date-based generic views from loading entire tables in memory when pagination is enabled.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17893 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
916d705538
commit
28e5b66518
|
@ -190,11 +190,16 @@ class BaseDateListView(MultipleObjectMixin, DateMixin, View):
|
||||||
date_field = self.get_date_field()
|
date_field = self.get_date_field()
|
||||||
allow_future = self.get_allow_future()
|
allow_future = self.get_allow_future()
|
||||||
allow_empty = self.get_allow_empty()
|
allow_empty = self.get_allow_empty()
|
||||||
|
paginate_by = self.get_paginate_by(qs)
|
||||||
|
|
||||||
if not allow_future:
|
if not allow_future:
|
||||||
qs = qs.filter(**{'%s__lte' % date_field: timezone.now()})
|
qs = qs.filter(**{'%s__lte' % date_field: timezone.now()})
|
||||||
|
|
||||||
if not allow_empty and not qs:
|
if not allow_empty:
|
||||||
|
# When pagination is enabled, it's better to do a cheap query
|
||||||
|
# than to load the unpaginated queryset in memory.
|
||||||
|
is_empty = not bool(qs) if paginate_by is None else not qs.exists()
|
||||||
|
if is_empty:
|
||||||
raise Http404(_(u"No %(verbose_name_plural)s available") % {
|
raise Http404(_(u"No %(verbose_name_plural)s available") % {
|
||||||
'verbose_name_plural': force_unicode(qs.model._meta.verbose_name_plural)
|
'verbose_name_plural': force_unicode(qs.model._meta.verbose_name_plural)
|
||||||
})
|
})
|
||||||
|
|
|
@ -78,6 +78,15 @@ class ArchiveIndexViewTests(TestCase):
|
||||||
self.assertEqual(res.context['page_obj'].number, 2)
|
self.assertEqual(res.context['page_obj'].number, 2)
|
||||||
self.assertEqual(list(res.context['latest']), list(Book.objects.all()[10:20]))
|
self.assertEqual(list(res.context['latest']), list(Book.objects.all()[10:20]))
|
||||||
|
|
||||||
|
def test_paginated_archive_view_does_not_load_entire_table(self):
|
||||||
|
# Regression test for #18087
|
||||||
|
self._make_books(20, base_date=datetime.date.today())
|
||||||
|
# 1 query for years list + 1 query for books
|
||||||
|
with self.assertNumQueries(2):
|
||||||
|
self.client.get('/dates/books/')
|
||||||
|
# same as above + 1 query to test if books exist
|
||||||
|
with self.assertNumQueries(3):
|
||||||
|
self.client.get('/dates/books/paginated/')
|
||||||
|
|
||||||
class YearArchiveViewTests(TestCase):
|
class YearArchiveViewTests(TestCase):
|
||||||
fixtures = ['generic-views-test-data.json']
|
fixtures = ['generic-views-test-data.json']
|
||||||
|
|
Loading…
Reference in New Issue