diff options
Diffstat (limited to 'django')
| -rw-r--r-- | django/views/generic/dates.py | 2 | ||||
| -rw-r--r-- | django/views/generic/list.py | 18 |
2 files changed, 15 insertions, 5 deletions
diff --git a/django/views/generic/dates.py b/django/views/generic/dates.py index 6964624516..00eadb71d5 100644 --- a/django/views/generic/dates.py +++ b/django/views/generic/dates.py @@ -273,7 +273,7 @@ class BaseDateListView(MultipleObjectMixin, DateMixin, View): 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() + is_empty = len(qs) == 0 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) diff --git a/django/views/generic/list.py b/django/views/generic/list.py index d4664c34ef..50127066a1 100644 --- a/django/views/generic/list.py +++ b/django/views/generic/list.py @@ -16,7 +16,7 @@ class MultipleObjectMixin(ContextMixin): def get_queryset(self): """ - Get the list of items for this view. This must be an interable, and may + Get the list of items for this view. This must be an iterable, and may be a queryset (in which qs-specific behavior will be enabled). """ if self.queryset is not None: @@ -113,9 +113,19 @@ class BaseListView(MultipleObjectMixin, View): def get(self, request, *args, **kwargs): self.object_list = self.get_queryset() allow_empty = self.get_allow_empty() - if not allow_empty and len(self.object_list) == 0: - raise Http404(_(u"Empty list and '%(class_name)s.allow_empty' is False.") - % {'class_name': self.__class__.__name__}) + + if not allow_empty: + # When pagination is enabled and object_list is a queryset, + # it's better to do a cheap query than to load the unpaginated + # queryset in memory. + if (self.get_paginate_by(self.object_list) is not None + and hasattr(self.object_list, 'exists')): + is_empty = not self.object_list.exists() + else: + is_empty = len(self.object_list) == 0 + if is_empty: + raise Http404(_(u"Empty list and '%(class_name)s.allow_empty' is False.") + % {'class_name': self.__class__.__name__}) context = self.get_context_data(object_list=self.object_list) return self.render_to_response(context) |
