更快的Django Admin Paginator:无法使此Django代码段正常工作 [英] Faster Django Admin Paginator: Cannot get this Django snippet to work

查看:195
本文介绍了更快的Django Admin Paginator:无法使此Django代码段正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django管理员列表中找到了用于提高大型数据库表查询性能的代码段:

I found this snippet for improving the performance of large database table queries in Django admin lists:

https://djangosnippets.org/snippets/2593/

在使用时存在一些问题它与Django 1.10一起使用,在我之前的问题中已经讨论过:

There are some issues about it when using it with Django 1.10, which are already discussed in my previous question here:

如何使用PostgreSQL计数估算来加快Django管理页面的速度?

尤其是, _count 需要重命名为 count query_set queryset 。这是该代码段相关部分的简短版本:

Particularly, _count needs to renamed to count and query_set to queryset. Here's a short version of the relevant part of the snippet:

from django.core.paginator import Paginator
from django.core.cache import cache
class FasterAdminPaginator(Paginator):
    def _get_count(self):
        if self.count is None:
            try:
                key = "adm:{0}:count".format( hash(self.object_list.query.__str__()) )
                self.count = cache.get(key, -1);
                if self.count == -1 :
                    if not self.object_list.query.where:
                        # estimates COUNT: https://djangosnippets.org/snippets/2593/
                        cursor = connection.cursor()
                        cursor.execute("SELECT reltuples FROM pg_class WHERE relname = %s",
                            [self.object_list.query.model._meta.db_table])
                        self.count = int(cursor.fetchone()[0])
                    else :
                        self.count = self.object_list.count()
                    cache.set(key, self.count, 3600)
            except:
                # AttributeError if object_list has no count() method.
                self.count = len(self.object_list)
        return self.count
    count = property(_get_count)

问题是,我仍然无法正常工作。当前错误日志摘录:

Problem is, I still cannot get it to work. Current error log excerpt:

maximum recursion depth exceeded while calling a Python object
...
result_count = paginator.count 
...
if self.count is None:

不知道如何使代码片段正常工作。

No idea how to get the snippet working.

推荐答案

我认为我们可以将分页器拖入django 1.10世界中(希望不是

I think we can drag that paginator into the django 1.10 world (hopefullly not kicking and screaming) like this:

from django.core.paginator import Paginator
from django.core.cache import cache
from django.utils.functional import cached_property
from django.db import connection

class FasterAdminPaginator(Paginator):
    @cached_property
    def count(self):
        try:
            if not self.object_list.query.where:
                # estimates COUNT: https://djangosnippets.org/snippets/2593/
                cursor = connection.cursor()
                cursor.execute("SELECT reltuples FROM pg_class WHERE relname = %s",
                    [self.object_list.query.model._meta.db_table])
                print 'Using the reltuples'

                ret = int(cursor.fetchone()[0])
            else :
                return self.object_list.count()
        except :
            import traceback
            traceback.print_exc()
            # AttributeError if object_list has no count() method.
            return len(self.object_list)

cached_property 不再需要原始摘要中使用的大量缓存代码。为了完整起见,这是 django.core的relevent部分。 .paginator.Paginator 看起来是

Thakns to cached_property the extensive caching code used in the original snippets are no longer needed. For completeness, this is what the relevent section of django.core.paginator.Paginator looks like

@cached_property
def count(self):
    """
    Returns the total number of objects, across all pages.
    """
    try:
        return self.object_list.count()
    except (AttributeError, TypeError):
        # AttributeError if object_list has no count() method.
        # TypeError if object_list.count() requires arguments
        # (i.e. is of type list).
        return len(self.object_list)

这篇关于更快的Django Admin Paginator:无法使此Django代码段正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆