Skip to content

Commit

Permalink
Merge f2f1ae9 into a5dda8a
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhegarty committed May 5, 2023
2 parents a5dda8a + f2f1ae9 commit 95b9fae
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
33 changes: 33 additions & 0 deletions docs/advanced_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,39 @@ column name (i.e. row header)::
:doc:`/api_fields`
Available field types and options.

Custom workflow based on update values
--------------------------------------

You can extend the import process to add workflow based on changes on incoming rows.

For example, suppose you are importing a list of books and you require additional workflow if the book is set to
'out of print'.

You can override the :meth:`~import_export.resources.Resource.after_import_instance` method to check if the
value changes::

class BookResource(resources.ModelResource):
def after_import_instance(self, instance, new, **kwargs):
# check to see if the 'out of print' value has changed in the import row
if not new and instance.is_out_of_print is False:
row = kwargs["row"]
is_out_of_print = row["out_of_print"]
# add custom workflow...

It might be a good idea to delay the custom workflow until after the instance has been saved successfully. To achieve
this, set a temporary flag on the instance, which can be read later in the import process::

class BookResource(resources.ModelResource):

def after_import_instance(self, instance, new, **kwargs):
if not new and instance.is_out_of_print is False:
row = kwargs["row"]
instance.is_out_of_print = row["out_of_print"]

def after_save_instance(self, instance, using_transactions, dry_run):
if getattr(instance, "is_out_of_print", False):
# add custom workflow

Field widgets
=============

Expand Down
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changelog

- Added support for Django 4.2 (#1570)
- Refactoring and fix to support filtering exports (#1579)
- pass `row` dict to :meth:`~import_export.resources.Resource.after_import_instance` (#1584)


3.2.0 (2023-04-12)
Expand Down
4 changes: 2 additions & 2 deletions import_export/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ def after_import_row(self, row, row_result, row_number=None, **kwargs):
"""
pass

def after_import_instance(self, instance, new, row_number=None, **kwargs):
def after_import_instance(self, instance, new, row_number=None, row=None, **kwargs):
"""
Override to add additional logic. Does nothing by default.
"""
Expand Down Expand Up @@ -757,7 +757,7 @@ def import_row(
try:
self.before_import_row(row, **kwargs)
instance, new = self.get_or_init_instance(instance_loader, row)
self.after_import_instance(instance, new, **kwargs)
self.after_import_instance(instance, new, row=row, **kwargs)
if new:
row_result.import_type = RowResult.IMPORT_TYPE_NEW
else:
Expand Down
15 changes: 15 additions & 0 deletions tests/core/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,21 @@ class Meta:
instance = resource.get_instance(instance_loader, self.dataset.dict[0])
self.assertEqual(instance, self.book)

def test_after_import_instance_row_param(self):
# issue 1583 - assert that row is passed to after_import_instance()
class BookResource(resources.ModelResource):
row = None

def after_import_instance(self, instance, new, row_number=None, **kwargs):
self.row = kwargs["row"]

class Meta:
model = Book

resource = BookResource()
resource.import_data(self.dataset, raise_errors=True)
self.assertEqual(self.dataset.dict[0], resource.row)

def test_get_instance_import_id_fields_with_custom_column_name(self):
class BookResource(resources.ModelResource):
name = fields.Field(
Expand Down

0 comments on commit 95b9fae

Please sign in to comment.