#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 |
Description
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
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
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 by , 14 years ago
milestone: | → 1.3 |
---|
comment:2 by , 14 years ago
comment:3 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
(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.
Another (and preferable, IMO) suggested fix:
In django.views.generic.list.MultipleObjectMixin.paginate_queryset#35
Replace this:
With this:
Currently, the pagination-related context variables
page_obj
andpaginator
(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 forpaginator
andpage_obj
to be present if the user specifies that they want to use a pagination by settingpaginate_by
. This also, coincidentally, resolves the issue mentioned in this ticket.