Commit 17557d06 authored by Thomas Chaumeny's avatar Thomas Chaumeny Committed by Tim Graham
Browse files

Fixed #8408 -- Added ModelAdmin.show_full_result_count to avoid COUNT() query.

Thanks lidaobing for the suggestion.
parent d1ca7011
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -110,6 +110,7 @@ class BaseModelAdmin(six.with_metaclass(forms.MediaDefiningClass)):
    readonly_fields = ()
    ordering = None
    view_on_site = True
    show_full_result_count = True

    # Validation of ModelAdmin definitions
    # Old, deprecated style:
+2 −2
Original line number Diff line number Diff line
@@ -87,9 +87,9 @@
      {% endif %}

      {% block result_list %}
          {% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}
          {% if action_form and actions_on_top and cl.show_admin_actions %}{% admin_actions %}{% endif %}
          {% result_list cl %}
          {% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}
          {% if action_form and actions_on_bottom and cl.show_admin_actions %}{% admin_actions %}{% endif %}
      {% endblock %}
      {% block pagination %}{% pagination cl %}{% endblock %}
      </form>
+1 −1
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@
<input type="text" size="40" name="{{ search_var }}" value="{{ cl.query }}" id="searchbar" />
<input type="submit" value="{% trans 'Search' %}" />
{% if show_result_count %}
    <span class="small quiet">{% blocktrans count counter=cl.result_count %}{{ counter }} result{% plural %}{{ counter }} results{% endblocktrans %} (<a href="?{% if cl.is_popup %}_popup=1{% endif %}">{% blocktrans with full_result_count=cl.full_result_count %}{{ full_result_count }} total{% endblocktrans %}</a>)</span>
    <span class="small quiet">{% blocktrans count counter=cl.result_count %}{{ counter }} result{% plural %}{{ counter }} results{% endblocktrans %} (<a href="?{% if cl.is_popup %}_popup=1{% endif %}">{% if cl.show_full_result_count %}{% blocktrans with full_result_count=cl.full_result_count %}{{ full_result_count }} total{% endblocktrans %}{% else %}{% trans "Show all" %}{% endif %}</a>)</span>
{% endif %}
{% for pair in cl.params.items %}
    {% ifnotequal pair.0 search_var %}<input type="hidden" name="{{ pair.0 }}" value="{{ pair.1 }}"/>{% endifnotequal %}
+10 −3
Original line number Diff line number Diff line
@@ -177,10 +177,13 @@ class ChangeList(object):
        # Perform a slight optimization:
        # full_result_count is equal to paginator.count if no filters
        # were applied
        if self.model_admin.show_full_result_count:
            if self.get_filters_params() or self.params.get(SEARCH_VAR):
                full_result_count = self.root_queryset.count()
            else:
                full_result_count = result_count
        else:
            full_result_count = None
        can_show_all = result_count <= self.list_max_show_all
        multi_page = result_count > self.list_per_page

@@ -194,6 +197,10 @@ class ChangeList(object):
                raise IncorrectLookupParameters

        self.result_count = result_count
        self.show_full_result_count = self.model_admin.show_full_result_count
        # Admin actions are shown if there is at least one entry
        # or if entries are not counted because show_full_result_count is disabled
        self.show_admin_actions = self.show_full_result_count or bool(full_result_count)
        self.full_result_count = full_result_count
        self.result_list = result_list
        self.can_show_all = can_show_all
+13 −0
Original line number Diff line number Diff line
@@ -1160,6 +1160,19 @@ subclass::
    :meth:`ModelAdmin.get_search_results` to provide additional or alternate
    search behavior.

.. attribute:: ModelAdmin.show_full_result_count

    .. versionadded:: 1.8

    Set ``show_full_result_count`` to control whether the full count of objects
    should be displayed on a filtered admin page (e.g. ``99 results (103 total)``).
    If this option is set to ``False``, a text like ``99 results (Show all)``
    is displayed instead.

    The default of ``show_full_result_count=True`` generates a query to perform
    a full count on the table which can be expensive if the table contains a
    large number of rows.

.. attribute:: ModelAdmin.view_on_site

    .. versionadded:: 1.7
Loading