Resources

Resource

class import_export.resources.Resource

Resource defines how objects are mapped to their import and export representations and handle importing and exporting data.

after_delete_instance(instance, dry_run)

Override to add additional logic. Does nothing by default.

after_export(queryset, data, *args, **kwargs)

Override to add additional logic. Does nothing by default.

after_import(dataset, result, using_transactions, dry_run, **kwargs)

Override to add additional logic. Does nothing by default.

after_import_instance(instance, new, **kwargs)

Override to add additional logic. Does nothing by default.

after_import_row(row, row_result, **kwargs)

Override to add additional logic. Does nothing by default.

after_save_instance(instance, using_transactions, dry_run)

Override to add additional logic. Does nothing by default.

before_delete_instance(instance, dry_run)

Override to add additional logic. Does nothing by default.

before_export(queryset, *args, **kwargs)

Override to add additional logic. Does nothing by default.

before_import(dataset, using_transactions, dry_run, **kwargs)

Override to add additional logic. Does nothing by default.

before_import_row(row, **kwargs)

Override to add additional logic. Does nothing by default.

before_save_instance(instance, using_transactions, dry_run)

Override to add additional logic. Does nothing by default.

delete_instance(instance, using_transactions=True, dry_run=False)

Calls instance.delete() as long as dry_run is not set.

export(queryset=None, *args, **kwargs)

Exports a resource.

for_delete(row, instance)

Returns True if row importing should delete instance.

Default implementation returns False. Override this method to handle deletion.

classmethod get_diff_class()

Returns the class used to display the diff for an imported instance.

get_diff_headers()

Diff representation headers.

classmethod get_error_result_class()

Returns the class used to store an error resulting from an import.

get_field_name(field)

Returns the field name for a given field.

get_fields(**kwargs)

Returns fields sorted according to export_order.

get_instance(instance_loader, row)

Calls the InstanceLoader.

get_or_init_instance(instance_loader, row)

Either fetches an already existing instance or initializes a new one.

classmethod get_result_class()

Returns the class used to store the result of an import.

classmethod get_row_result_class()

Returns the class used to store the result of a row import.

import_data(dataset, dry_run=False, raise_errors=False, use_transactions=None, collect_failed_rows=False, **kwargs)

Imports data from tablib.Dataset. Refer to Import data workflow for a more complete description of the whole import process.

Parameters:
  • dataset – A tablib.Dataset
  • raise_errors – Whether errors should be printed to the end user or raised regularly.
  • use_transactions – If True the import process will be processed inside a transaction.
  • collect_failed_rows – If True the import process will collect failed rows.
  • dry_run – If dry_run is set, or an error occurs, if a transaction is being used, it will be rolled back.
import_field(field, obj, data, is_m2m=False)

Calls import_export.fields.Field.save() if Field.attribute and Field.column_name are found in data.

import_obj(obj, data, dry_run)

Traverses every field in this Resource and calls import_field(). If import_field() results in a ValueError being raised for one of more fields, those errors are captured and reraised as a single, multi-field ValidationError.

import_row(row, instance_loader, using_transactions=True, dry_run=False, **kwargs)

Imports data from tablib.Dataset. Refer to Import data workflow for a more complete description of the whole import process.

Parameters:
  • row – A dict of the row to import
  • instance_loader – The instance loader to be used to load the row
  • using_transactions – If using_transactions is set, a transaction is being used to wrap the import
  • dry_run – If dry_run is set, or error occurs, transaction will be rolled back.
save_instance(instance, using_transactions=True, dry_run=False)

Takes care of saving the object to the database.

Keep in mind that this is done by calling instance.save(), so objects are not created in bulk!

save_m2m(obj, data, using_transactions, dry_run)

Saves m2m fields.

Model instance need to have a primary key value before a many-to-many relationship can be used.

skip_row(instance, original)

Returns True if row importing should be skipped.

Default implementation returns False unless skip_unchanged == True. Override this method to handle skipping rows meeting certain conditions.

Use super if you want to preserve default handling while overriding

class YourResource(ModelResource):
    def skip_row(self, instance, original):
        # Add code here
        return super(YourResource, self).skip_row(instance, original)
validate_instance(instance, import_validation_errors=None, validate_unique=True)

Takes any validation errors that were raised by import_obj(), and combines them with validation errors raised by the instance’s full_clean() method. The combined errors are then re-raised as single, multi-field ValidationError.

If the clean_model_instances option is False, the instances’s full_clean() method is not called, and only the errors raised by import_obj() are re-raised.

ModelResource

class import_export.resources.ModelResource

ModelResource is Resource subclass for handling Django models.

DEFAULT_RESOURCE_FIELD

alias of import_export.fields.Field

after_import(dataset, result, using_transactions, dry_run, **kwargs)

Reset the SQL sequences after new objects are imported

classmethod field_from_django_field(field_name, django_field, readonly)

Returns a Resource Field instance for the given Django model field.

classmethod get_fk_widget(field)

Prepare widget for fk and o2o fields

get_import_id_fields()
classmethod get_m2m_widget(field)

Prepare widget for m2m field

get_queryset()

Returns a queryset of all objects for this model. Override this if you want to limit the returned queryset.

init_instance(row=None)

Initializes a new Django model.

classmethod widget_from_django_field(f, default=<class 'import_export.widgets.Widget'>)

Returns the widget that would likely be associated with each Django type.

Includes mapping of Postgres Array and JSON fields. In the case that psycopg2 is not installed, we consume the error and process the field regardless.

classmethod widget_kwargs_for_field(field_name)

Returns widget kwargs for given field_name.

ResourceOptions (Meta)

class import_export.resources.ResourceOptions

The inner Meta class allows for class-level configuration of how the Resource should behave. The following options are available:

clean_model_instances = False

Controls whether instance.full_clean() is called during the import process to identify potential validation errors for each (non skipped) row. The default value is False.

exclude = None

Controls what introspected fields the Resource should NOT include. A blacklist of fields.

export_order = None

Controls export order for columns.

fields = None

Controls what introspected fields the Resource should include. A whitelist of fields.

import_id_fields = ['id']

Controls which object fields will be used to identify existing instances.

instance_loader_class = None

Controls which class instance will take care of loading existing objects.

model = None

Django Model class. It is used to introspect available fields.

report_skipped = True

Controls if the result reports skipped rows Default value is True

skip_unchanged = False

Controls if the import should skip unchanged records. Default value is False

use_transactions = None

Controls if import should use database transactions. Default value is None meaning settings.IMPORT_EXPORT_USE_TRANSACTIONS will be evaluated.

widgets = None

This dictionary defines widget kwargs for fields.

modelresource_factory

resources.modelresource_factory(resource_class=<class 'import_export.resources.ModelResource'>)

Factory for creating ModelResource class for given Django model.