Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#14873 closed (fixed)

A paginated ListView with a List instead of queryset produces an error

Reported by: andornaut Owned by: nobody
Component: Generic views Version: 1.3-alpha
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no



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


    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).


    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()

Change History (4)

comment:1 Changed 8 years ago by Chris Beaven

milestone: 1.3

comment:2 Changed 8 years ago by andornaut

Another (and preferable, IMO) suggested fix:

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

Replace this:

if queryset.count() > page_size:

With this:

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.

comment:3 Changed 8 years ago by Chris Beaven

Resolution: fixed
Status: newclosed

(In [14864]) Fixes #14873 -- A paginated ListView with a List instead of queryset produces an error.

Additional minor change in functionality: the page is now not considered paginated if the objects do not span multiple pages according to the paginator. This will only affect views with a custom paginator method which uses orphans.

comment:4 Changed 7 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

Note: See TracTickets for help on using tickets.
Back to Top