[Django] #14873: A paginated ListView with a List instead of queryset produces an error

37 views
Skip to first unread message

Django

unread,
Dec 8, 2010, 7:56:48 PM12/8/10
to djang...@holovaty.com, django-...@googlegroups.com
#14873: A paginated ListView with a List instead of queryset produces an error
---------------------------+------------------------------------------------
Reporter: andornaut | Owner: nobody
Status: new | Milestone:
Component: Generic views | Version: 1.3-alpha
Keywords: | Stage: Unreviewed
Has_patch: 0 |
---------------------------+------------------------------------------------
== Background ==

This issue occurs when using a subclass of `ListView` that overrides the
`get_queryset` method and returns a `List` instead of a `queryset`, while
also defining `paginage_by`. This should work according to the docstring
for `get_queryset`, but it produces an exception.

== Relevant Code ==
django.views.generic.list.!MultipleObjectMixin.get_queryset!#15
{{{
#!python
def get_queryset(self):
"""
Get the list of items for this view. This must be an interable,
and may
be a queryset (in which qs-specific behavior will be enabled).
}}}

django.views.generic.list.!MultipleObjectMixin.paginate_queryset!#31
{{{
#!python
def paginate_queryset(self, queryset, page_size):
"""
Paginate the queryset, if needed.
"""
if queryset.count() > page_size:
}}}

The error occurs when trying to evaluate `queryset.count()` on a List.

== Possible Solution ==

If a non-queryset Iterable is used, then use `len(queryset)` instead of
`queryset.count()`

--
Ticket URL: <http://code.djangoproject.com/ticket/14873>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 8, 2010, 8:06:29 PM12/8/10
to djang...@holovaty.com, django-...@googlegroups.com
#14873: A paginated ListView with a List instead of queryset produces an error
------------------------------------+---------------------------------------
Reporter: andornaut | Owner: nobody
Status: new | Milestone: 1.3
Component: Generic views | Version: 1.3-alpha
Resolution: | Keywords:
Stage: Unreviewed | Has_patch: 0
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
------------------------------------+---------------------------------------
Changes (by SmileyChris):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* milestone: => 1.3

--
Ticket URL: <http://code.djangoproject.com/ticket/14873#comment:1>

Django

unread,
Dec 8, 2010, 8:29:27 PM12/8/10
to djang...@holovaty.com, django-...@googlegroups.com
#14873: A paginated ListView with a List instead of queryset produces an error
------------------------------------+---------------------------------------
Reporter: andornaut | Owner: nobody
Status: new | Milestone: 1.3
Component: Generic views | Version: 1.3-alpha
Resolution: | Keywords:
Stage: Unreviewed | Has_patch: 0
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
------------------------------------+---------------------------------------
Comment (by andornaut):

Another (and preferable, IMO) suggested fix:

In django.views.generic.list.!MultipleObjectMixin.paginate_queryset!#35

Replace this:
{{{
#!python
if queryset.count() > page_size:
}}}
With this:
{{{
#!python
if hasattr(self, 'paginate_by') and self.paginate_by:
}}}

Currently, the pagination-related context variables `page_obj` and
`paginator` (line 91 of the same module) are only included if the number
of pages is greater than 1. This requires that the template author handle
this case in templates that use page_obj.has_next and other variables.
Previously (IIRC), in Django 1.2's function-based generic views, the
template author could count on those variables always being present, and
so s/he didn't have to handle the special case where there is only 1 page.
I believe that it would be preferable for `paginator` and `page_obj` to
be present if the user specifies that they want to use a pagination by
setting `paginate_by`. This also, coincidentally, resolves the issue
mentioned in this ticket.

--
Ticket URL: <http://code.djangoproject.com/ticket/14873#comment:2>
Reply all
Reply to author
Forward
0 new messages