#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:  master
     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 michi88):

 I indeed spoke too early. This did work for me as I combine it with
 `django-postgres-fuzzycount` as well which takes care of the unfiltered
 total count:

 https://github.com/stephenmcd/django-postgres-
 fuzzycount/blob/master/fuzzycount.py

 {{{
 #!python
 class FuzzyCountAdmin(ReplicaModelAdmin):
     """
      Models must have:

      fuzzy_count_manager = FuzzyCountManager()
     """

     def get_queryset(self, request):
         if hasattr(self.model, 'fuzzy_count_manager') and request.method
 == 'GET':
             qs = self.model.fuzzy_count_manager.get_queryset()
         else:
             qs = self.model._default_manager.get_queryset()
         # taken from Django source
         # TODO: this should be handled by some parameter to the
 ChangeList.
         ordering = self.get_ordering(request)
         if ordering:
             qs = qs.order_by(*ordering)
         return qs
 }}}

 This also works for the filtered requests:
 {{{
 #!python
 class LenCountPaginator(Paginator):

     @cached_property
     def count(self):
         if self.object_list.query.where or getattr(self.object_list.query,
 'having', None):
             return len(self.object_list)
         return self.object_list.count()
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/8408#comment:52>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.05cc5a9937fead2aac122cff81400530%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to