Skip to content

Instantly share code, notes, and snippets.

@clint74
Created October 24, 2019 21:47
Show Gist options
  • Save clint74/bd74025e799bf7101a5471b08ecd1567 to your computer and use it in GitHub Desktop.
Save clint74/bd74025e799bf7101a5471b08ecd1567 to your computer and use it in GitHub Desktop.
# Using django import_export with transactions
# the whole was about 40 minutes before change, now 5 minutes.
from import_export.utils import atomic_if_using_transaction
from django.db import connections, DEFAULT_DB_ALIAS
from django.core.exceptions import ImproperlyConfigured
class PubliResource(resources.ModelResource):
use_transactions_in_dry_run = False # <=====
def import_data(self, dataset, dry_run=False, raise_errors=False,
use_transactions=None, collect_failed_rows=False, **kwargs):
"""
Imports data from ``tablib.Dataset``. Refer to :doc:`import_workflow`
for a more complete description of the whole import process.
:param dataset: A ``tablib.Dataset``
:param raise_errors: Whether errors should be printed to the end user
or raised regularly.
:param use_transactions: If ``True`` the import process will be processed
inside a transaction.
:param collect_failed_rows: If ``True`` the import process will collect
failed rows.
:param dry_run: If ``dry_run`` is set, or an error occurs, if a transaction
is being used, it will be rolled back.
"""
if use_transactions is None:
use_transactions = self.get_use_transactions()
connection = connections[DEFAULT_DB_ALIAS]
supports_transactions = getattr(connection.features, "supports_transactions", False)
if use_transactions and not supports_transactions:
raise ImproperlyConfigured
# using_transactions = (use_transactions or dry_run) and supports_transactions
# Now it looks for local attribute to force no transaction mode <===
using_transactions = supports_transactions and (
use_transactions or
(dry_run and self.use_transactions_in_dry_run)
)
with atomic_if_using_transaction(using_transactions):
return self.import_data_inner(dataset, dry_run, raise_errors, using_transactions, collect_failed_rows, **kwargs)
class Meta:
model = Publi
# other stuff ...
#Import Export
IMPORT_EXPORT_USE_TRANSACTIONS = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment