Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #18087 -- Prevented date-based generic views from loading entir…

…e tables in memory when pagination is enabled.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17893 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 28e5b665189548557993b522e3bd7adeed3f62ea 1 parent 916d705
@aaugustin aaugustin authored
View
13 django/views/generic/dates.py
@@ -190,14 +190,19 @@ def get_dated_queryset(self, **lookup):
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
9 tests/regressiontests/generic_views/dates.py
@@ -78,6 +78,15 @@ def test_paginated_archive_view(self):
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']
Please sign in to comment.
Something went wrong with that request. Please try again.