Resources¶
Resource¶
-
class
import_export.resources.
Resource
(**kwargs)¶ 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 asdry_run
is not set. Ifuse_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
ifrow
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, rollback_on_validation_errors=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. - rollback_on_validation_errors – If both
use_transactions
androllback_on_validation_errors
are set toTrue
, the import process will be rolled back in case of ValidationError. - dry_run – If
dry_run
is set, or an error occurs, if a transaction is being used, it will be rolled back.
- dataset – A
-
import_field
(field, obj, data, is_m2m=False, **kwargs)¶ Calls
import_export.fields.Field.save()
ifField.attribute
is specified, andField.column_name
is found indata
.
-
import_obj
(obj, data, dry_run, **kwargs)¶ Traverses every field in this Resource and calls
import_field()
. Ifimport_field()
results in aValueError
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.
- row – A
-
init_instance
(row=None)¶ Initializes an object. Implemented in
import_export.resources.ModelResource.init_instance()
.
-
save_instance
(instance, is_create, 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.Parameters: - instance – The instance of the object to be persisted.
- is_create – A boolean flag to indicate whether this is a new object
to be created, or an existing object to be updated. :param using_transactions: A flag to indicate whether db transactions are used. :param dry_run: A flag to indicate dry-run mode.
-
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, row, import_validation_errors=None)¶ Returns
True
ifrow
importing should be skipped.Default implementation returns
False
unless skip_unchanged == True and skip_diff == False.If skip_diff is True, then no comparisons can be made because
original
will be None.When left unspecified, skip_diff and skip_unchanged both default to
False
, and rows are never skipped.By default, rows are not skipped if validation errors have been detected during import. You can change this behavior and choose to ignore validation errors by overriding this method.
Override this method to handle skipping rows meeting certain conditions.
Use
super
if you want to preserve default handling while overridingclass YourResource(ModelResource): def skip_row(self, instance, original, row, import_validation_errors=None): # Add code here return super().skip_row(instance, original, row, import_validation_errors=import_validation_errors)
-
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’sfull_clean()
method. The combined errors are then re-raised as single, multi-field ValidationError.If the
clean_model_instances
option is False, the instances’sfull_clean()
method is not called, and only the errors raised byimport_obj()
are re-raised.
-
ModelResource¶
-
class
import_export.resources.
ModelResource
(**kwargs)¶ 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 field. 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 toTrue
improves performance, because the copy and comparison operations are skipped for each row. If enabled, thenskip_row()
checks do not execute, because ‘skip’ logic requires comparison between the stored and imported versions of a row. If enabled, then HTML row reports are also not generated (seeskip_html_diff
). The default value is False.
-
skip_html_diff
= False¶ Controls whether or not a HTML report is generated after each row. By default, the difference between a stored copy and an imported instance is generated in HTML form and stored in each
RowResult
. The HTML report is used to present changes on the confirmation screen in the admin site, hence when this value isTrue
, then changes will not be presented on the confirmation screen. If the HTML report is not required, then setting this value toTrue
improves performance, because the HTML generation is skipped for each row. This is a useful optimization when importing large datasets. The default value is False.
-
skip_unchanged
= False¶ Controls if the import should skip unchanged records. Default value is False
-
store_row_values
= False¶ If True, each row’s raw data will be stored in each row result. Enabling this parameter will increase the memory usage during import which should be considered when importing large datasets.
-
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_natural_foreign_keys
= False¶ If True, use_natural_foreign_keys = True will be passed to all foreign key widget fields whose models support natural foreign keys. That is, the model has a natural_key function and the manager has a get_by_natural_key function.
-
use_transactions
= None¶ Controls if import should use database transactions. Default value is
None
meaningsettings.IMPORT_EXPORT_USE_TRANSACTIONS
will be evaluated.
-
using_db
= None¶ DB Connection name to use for db transactions. If not provided,
router.db_for_write(model)
will be evaluated and if it’s missing, DEFAULT_DB_ALIAS constant (“default”) is used.
-
widgets
= None¶ This dictionary defines widget kwargs for fields.
-