diff --git a/django/views/generic/dates.py b/django/views/generic/dates.py index e5c3e5bbe3..e1daa9f04d 100644 --- a/django/views/generic/dates.py +++ b/django/views/generic/dates.py @@ -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 diff --git a/tests/regressiontests/generic_views/dates.py b/tests/regressiontests/generic_views/dates.py index 652f66b7f1..9de817acc2 100644 --- a/tests/regressiontests/generic_views/dates.py +++ b/tests/regressiontests/generic_views/dates.py @@ -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']