を動作させることはできません私は、Djangoの管理者リストに大規模なデータベーステーブルのクエリのパフォーマンスを向上させるため、このスニペットを見つけました:高速化Djangoの管理ページネータは:このDjangoのスニペットは
https://djangosnippets.org/snippets/2593/
を使用している場合、それについていくつかの問題があります。に改名へ
How to speed up Django's admin pages with PostgreSQL count estimates?
特に、_count
ニーズ:すでにここで私の前の質問で説明されているDjangoの1.10、とのことcount
およびquery_set
~queryset
。スニペットの関連部分の短いバージョンです:
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)
問題は、私はまだそれを動作させることはできません。現在のエラーログ抜粋:
maximum recursion depth exceeded while calling a Python object
...
result_count = paginator.count
...
if self.count is None:
スニペットの仕組みを理解できません。
魅力のように動作します。ありがとう。楽しく、私は基本的に同時にスニペットの問題を発見しました。 try-exceptのために、私は単に 'connection'の欠落したインポートに気付かなかっただけです。 –
はい、それはかなり悪いことでした。自分で同時発見を行ったにもかかわらず、答えを正しいとマークしてくれてありがとう – e4c5