Django

Code

Ticket #7478 (closed: fixed)

Opened 6 months ago

Last modified 5 months ago

Paginator could detect if the object_list is a QuerySet or a list

Reported by: batiste@dosimple.ch Assigned to: nobody
Milestone: 1.0 Component: Core framework
Version: SVN Keywords: paginator
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

If the Paginator is accidentally used to count a large QuerySet? (instead of a QuerySetPaginator?), the generated SQL request could be inefficient (all content will be retrieved just to make len on it). Why don't we automatically detect if object_list has a count attribute? The paginator API will be simpler and it will be more difficult to introduce performance issues.

    def _get_count(self):
        if self._count is None:
            if hasattr(self.object_list, 'count'):
                self._count = self.object_list.count()
            else:
                self._count = len(self.object_list)
        return self._count
    count = property(_get_count)

And by the way the Paginator is far way to simple for my needs so I have sub class it. But I can't derivate from both QuerySetPaginator? and Paginator. It's antoher reason why I think it's not a good idea to have 2 classes.

Attachments

Change History

06/17/08 03:58:52 changed by anonymous

  • keywords set to paginator.
  • needs_better_patch changed.
  • component changed from Uncategorized to Core framework.
  • needs_tests changed.
  • needs_docs changed.

06/17/08 05:01:24 changed by anonymous

Woops count is not the good attribute to test because lists also have it. "all" seems a safe choice to me.

06/17/08 05:20:54 changed by telenieko

type(xxx) == QuerySet? ... ?

06/17/08 06:13:23 changed by anonymous

if isinstance(self.object_list, QuerySet):
    self._count = self.object_list.count()

06/18/08 07:29:47 changed by anonymous

I am using this one and it works nicely:

    def _get_count(self):
        if self._count is None:
            if isinstance(self.object_list, QuerySet):
                self._count = self.object_list.count()
            else:
                self._count = len(self.object_list)
        return self._count
    count = property(_get_count)

06/29/08 04:13:54 changed by mrts

  • milestone set to 1.0.

This looks reasonable and in scope for 1.0.

07/07/08 21:08:34 changed by adrian

  • status changed from new to closed.
  • resolution set to fixed.

(In [7865]) Fixed #7478 -- Rolled QuerySetPaginator? into the Paginator class, to simplify things. QuerySetPaginator? still exists as an alias, for backwards compatibility. Thanks for the suggestion, batiste@dosimple.ch


Add/Change #7478 (Paginator could detect if the object_list is a QuerySet or a list)




Change Properties
Action