我有一张大桌子,目前约有40万页,并且还在不断增长。管理页面已经达到了要点,即试图查看它时只是超时。我添加了一个分页器,但这似乎没有帮助。任何建议,将不胜感激。
class MyAdmin(admin.ModelAdmin):
...
paginator = LargeTablePaginator
...
class LargeTablePaginator(Paginator):
@property
def count(self):
query = self.object_list.query
if not query.where:
cursor = connection.cursor()
cursor.execute('SELECT reltuples FROM pg_class WHERE relname = %s', [query.model._meta.db_table])
return int(cursor.fetchone()[0])
我还尝试过更新get_queryset方法,使其仅返回前24小时的数据,但这并没有缩短加载时间
def get_queryset(self, request):
return super(MyAdmin, self).get_queryset(request).filter(created_on__gte=datetime.datetime.today())
我的模特
class MyModel(models.Model):
class Meta:
db_table = "model_table"
name = models.CharField(unique=True, max_length=255, blank=True)
created_on = models.DateTimeField(auto_now_add=True, null=True)
content = models.CharField(max_length=512, blank=True)