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, row_number=None, **kwargs)

Override to add additional logic. Does nothing by default.

after_import_row(row, row_result, row_number=None, **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, row_number=None, **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.

bulk_create(using_transactions, dry_run, raise_errors, batch_size=None)

Creates objects by calling bulk_create.

bulk_delete(using_transactions, dry_run, raise_errors)

Deletes objects by filtering on a list of instances to be deleted, then calling delete() on the entire queryset.

bulk_update(using_transactions, dry_run, raise_errors, batch_size=None)

Updates objects by calling bulk_update.

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

Calls instance.delete() as long as dry_run is not set. If use_bulk then instances are appended to a list for bulk import.

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.

get_bulk_update_fields()

Returns the fields to be included in calls to bulk_update(). import_id_fields are removed because id fields cannot be supplied to bulk_update().

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_import_id_fields()
get_instance(instance_loader, row)

If all ‘import_id_fields’ are present in the dataset, calls the InstanceLoader. Otherwise, returns None.

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, raise_errors=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.
init_instance(row=None)

Initializes an object. Implemented in import_export.resources.ModelResource.init_instance().

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

Takes care of saving the object to the database.

Objects can be created in bulk if use_bulk is enabled.

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, or skip_diff == True.

If skip_diff is True, then no comparisons can be made because original will be None.

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

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:

batch_size = 1000

The batch_size parameter controls how many objects are created in a single query. The default is to create objects in batches of 1000. See bulk_create(). This parameter is only used if use_bulk is True.

chunk_size = None

Controls the chunk_size argument of Queryset.iterator or, if prefetch_related is used, the per_page attribute of Paginator.

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.

force_init_instance = False

If True, this parameter will prevent imports from checking the database for existing instances. Enabling this parameter is a performance enhancement if your import dataset is guaranteed to contain new instances.

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_diff = False

Controls whether or not an instance should be diffed following import. By default, an instance is copied prior to insert, update or delete. After each row is processed, the instance’s copy is diffed against the original, and the value stored in each RowResult. If diffing is not required, then disabling the diff operation by setting this value to True improves performance, because the copy and comparison operations are skipped for each row. The default value is False.

skip_unchanged = False

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

use_bulk = False

Controls whether import operations should be performed in bulk. By default, an object’s save() method is called for each row in a data set. When bulk is enabled, objects are saved using bulk operations.

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.