#8408: Add a new meta option: don't do count(*) in admin
-------------------------------+-------------------------------------------
     Reporter:  LI Daobing     |                    Owner:  Thomas Chaumeny
         Type:  New feature    |                   Status:  new
    Component:  contrib.admin  |                  Version:  dev
     Severity:  Normal         |               Resolution:
     Keywords:                 |             Triage Stage:  Accepted
    Has patch:  1              |      Needs documentation:  0
  Needs tests:  1              |  Patch needs improvement:  1
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+-------------------------------------------

Comment (by msphn):

 I couldn't find any good solution so I build myself a custom paginator.

 My database is mysql using innodb, I made use of innodb to get a fast and
 precise enough count of rows. Please be aware, that this might not be
 precise enough for everyone's usecase.

 My paginator shouldn't be understood as a patch, but it's an adequate
 workaround to speed up your django admin. I had no problem with that, and
 I can now easily browse my 5 million+ datasets.

 {{{#!python
 from django.core.paginator import Paginator

 from django.utils.functional import cached_property
 from django.db import connection


 class MysqlInnoDbPaginator(Paginator):
     @cached_property
     def count(self):
         with connection.cursor() as cursor:
             cursor.execute(
                 "SELECT TABLE_ROWS FROM information_schema.tables WHERE
 TABLE_SCHEMA = %s AND TABLE_NAME = %s;",
                 [connection.settings_dict['NAME'],
 self.object_list.model._meta.db_table]
             )

             row = cursor.fetchone()

             if row is not None:
                 return row[0]
             else:
                 return 0
 }}}


 Add this to your admin.ModelAdmin implementation. The
 show_full_result_count is very important, otherwise another count would
 happen.

 {{{#!python
 class FoobarAdmin(admin.ModelAdmin):
     paginator = MysqlInnoDbPaginator
     show_full_result_count = False
 }}}

 Also be aware, if you have multiple database configurations, you should
 modify it so it would find the right database name

-- 
Ticket URL: <https://code.djangoproject.com/ticket/8408#comment:59>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070180bd81c17d-ba8a5de7-9695-4cf2-9864-ea9468428a09-000000%40eu-central-1.amazonses.com.

Reply via email to