Skip to content

Commit

Permalink
Merge 616eb3b into b63ddd6
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Dec 14, 2023
2 parents b63ddd6 + 616eb3b commit fdf2398
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Changelog
------------------

- Nothing changed yet.

- Remove unnecessary COUNT queries to speed up export

3.3.4 (2023-12-09)
------------------
Expand Down
26 changes: 25 additions & 1 deletion import_export/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import warnings
from contextlib import contextmanager

import django
from django import forms
Expand Down Expand Up @@ -69,6 +70,24 @@ def changelist_view(self, request, extra_context=None):
return super().changelist_view(request, extra_context)


class FakePaginator:
count = 0


def _get_paginator(request, queryset, per_page):
return FakePaginator()


@contextmanager
def temp_attr(obj, attr_name, new_value):
original_value = getattr(obj, attr_name)
setattr(obj, attr_name, new_value)
try:
yield
finally:
setattr(obj, attr_name, original_value)


class ImportMixin(BaseImportMixin, ImportExportMixinBase):
"""
Import mixin.
Expand Down Expand Up @@ -741,7 +760,12 @@ def get_export_queryset(self, request):
changelist_kwargs["sortable_by"] = self.sortable_by
if django.VERSION >= (4, 0):
changelist_kwargs["search_help_text"] = self.search_help_text
cl = ChangeList(**changelist_kwargs)

# Temporarily set to False to avoid unnecessary COUNT queries.
with temp_attr(self, "show_full_result_count", False):
# Temporarily set to FakePaginator to avoid unnecessary COUNT queries.
with temp_attr(self, "get_paginator", _get_paginator):
cl = ChangeList(**changelist_kwargs)

return cl.get_queryset(request)

Expand Down
3 changes: 2 additions & 1 deletion tests/core/tests/test_admin_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,8 @@ def test_export(self):
"file_format": "0",
}
date_str = datetime.now().strftime("%Y-%m-%d")
response = self.client.post("/admin/core/book/export/", data)
with self.assertNumQueries(7): # Should not contain COUNT queries from ModelAdmin.get_results()
response = self.client.post("/admin/core/book/export/", data)
self.assertEqual(response.status_code, 200)
self.assertTrue(response.has_header("Content-Disposition"))
self.assertEqual(response["Content-Type"], "text/csv")
Expand Down

0 comments on commit fdf2398

Please sign in to comment.