Skip to content

Commit

Permalink
Merge pull request #280 from VSEScala/mhvis/202401/fix-csv-speed
Browse files Browse the repository at this point in the history
Fix slow download speed of the transactions CSV file
  • Loading branch information
mhvis authored Jan 1, 2024
2 parents 80d1813 + 0805f70 commit 94d72a9
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions creditmanagement/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import csv
from typing import Iterator

from django.db.models import QuerySet
from django.utils.timezone import localdate


Expand All @@ -12,12 +13,24 @@ def write(self, value):
return value


def transactions_csv(transactions) -> Iterator:
def transactions_csv(transactions: QuerySet) -> Iterator:
"""Returns an iterator that yields the transaction CSV rows.
Args:
transactions: List or QuerySet of transactions.
transactions: A QuerySet of transactions. Cannot be a list, because we will
modify the query to fetch related fields.
"""
# This speeds up the query by >100x
transactions = transactions.select_related(
"source",
"target",
"created_by",
"source__user",
"source__association",
"target__user",
"target__association",
)

writer = csv.writer(Echo())
yield writer.writerow(
[
Expand Down

0 comments on commit 94d72a9

Please sign in to comment.