0
0
mirror of https://github.com/django/django.git synced 2024-12-01 15:42:04 +01:00

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:
Aymeric Augustin 2012-04-10 20:05:46 +00:00
parent 916d705538
commit 28e5b66518
2 changed files with 18 additions and 4 deletions

View File

@ -190,14 +190,19 @@ class BaseDateListView(MultipleObjectMixin, DateMixin, View):
date_field = self.get_date_field()
allow_future = self.get_allow_future()
allow_empty = self.get_allow_empty()
paginate_by = self.get_paginate_by(qs)
if not allow_future:
qs = qs.filter(**{'%s__lte' % date_field: timezone.now()})
if not allow_empty and not qs:
raise Http404(_(u"No %(verbose_name_plural)s available") % {
'verbose_name_plural': force_unicode(qs.model._meta.verbose_name_plural)
})
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") % {
'verbose_name_plural': force_unicode(qs.model._meta.verbose_name_plural)
})
return qs

View File

@ -78,6 +78,15 @@ class ArchiveIndexViewTests(TestCase):
self.assertEqual(res.context['page_obj'].number, 2)
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):
fixtures = ['generic-views-test-data.json']