Resource defines how objects are mapped to their import and export representations and handle importing and exporting data.
An optional dict of kwargs. Subclasses can use kwargs to pass dynamic values to enhance import / exports.
Override to add additional logic. Does nothing by default.
instance – A new or existing model instance.
row – A dict
containing key / value data for the row to be imported.
**kwargs – See import_row()
Override to add additional logic. Does nothing by default.
queryset – The queryset for export.
dataset – A tablib.Dataset
.
**kwargs – Metadata which may be associated with the export.
Override to add additional logic. Does nothing by default.
dataset – A tablib.Dataset
.
result – A import_export.results.Result
implementation containing a summary of the import.
**kwargs – See import_row()
Override to add additional logic. Does nothing by default.
row – A dict
containing key / value data for the row to be imported.
row_result – A RowResult
instance. References the persisted instance
as an attribute.
**kwargs – See import_row()
Override to add additional logic. Does nothing by default.
instance – A new or existing model instance.
new – a boolean flag indicating whether instance is new or existing.
row – A dict
containing key / value data for the row to be imported.
**kwargs – See import_row()
Override to add additional logic. Does nothing by default.
instance – A new or existing model instance.
row – A dict
containing key / value data for the row to be imported.
**kwargs – See import_row()
Override to add additional logic. Does nothing by default.
instance – A new or existing model instance.
row – A dict
containing key / value data for the row to be imported.
**kwargs – See import_row()
Override to add additional logic. Does nothing by default.
queryset – The queryset for export.
**kwargs – Metadata which may be associated with the export.
Override to add additional logic. Does nothing by default.
dataset – A tablib.Dataset
.
**kwargs – See import_row()
Override to add additional logic. Does nothing by default.
row – A dict
containing key / value data for the row to be imported.
**kwargs – See import_row()
Override to add additional logic. Does nothing by default.
instance – A new or existing model instance.
row – A dict
containing key / value data for the row to be imported.
**kwargs – See import_row()
Creates objects by calling bulk_create
.
Deletes objects by filtering on a list of instances to be deleted, then calling delete()
on the entire queryset.
Updates objects by calling bulk_update
.
Calls instance.delete()
as long as dry_run
is not set. If use_bulk
then instances are appended to a list for bulk import.
instance – A new or existing model instance.
row – A dict
containing key / value data for the row to be imported.
**kwargs – See import_row()
A method specifically to provide a single overridable hook for the instance save operation. For example, this can be overridden to implement update_or_create().
instance – The model instance to be saved.
is_create – A boolean flag to indicate whether this is a new object to be created, or an existing object to be updated.
Exports a resource.
queryset – The queryset for export (optional).
A tablib.Dataset
.
Override to filter an export queryset.
queryset – The queryset for export.
**kwargs – Metadata which may be associated with the export.
The filtered queryset.
Returns True
if row
importing should delete instance.
Default implementation returns False
. Override this method to handle deletion.
row – A dict
containing key / value data for the row to be imported.
instance – A new or existing model instance.
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().
Returns the class used to display the diff for an imported instance.
Diff representation headers.
Returns the class used to store an error resulting from an import.
Returns the field name for a given field.
Calls the InstanceLoader.
Either fetches an already existing instance or initializes a new one.
Returns the class used to store the result of an import.
Returns the class used to store the result of a row import.
Imports data from tablib.Dataset
. Refer to Import workflow for a more complete description of the whole import process.
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 create a new dataset object comprising failed rows and errors. This can be useful for debugging purposes but will cause higher memory usage for larger datasets. See failed_dataset
.
rollback_on_validation_errors – If both use_transactions
and rollback_on_validation_errors
are set to True
, 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.
**kwargs – Metadata which may be associated with the import.
Handles persistence of the field data.
field – A import_export.fields.Field
instance.
instance – A new or existing model instance.
row – A dict
containing key / value data for the row to be imported.
is_m2m – A boolean value indicating whether or not this is a many-to-many field.
**kwargs – See import_row()
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.
instance – A new or existing model instance.
row – A dict
containing key / value data for the row to be imported.
**kwargs – See import_row()
Imports data from tablib.Dataset
. Refer to Import workflow for a more complete description of the whole import process.
row – A dict
of the ‘row’ to import. A row is a dict of data fields so can be a csv line, a JSON object, a YAML object etc.
instance_loader – The instance loader to be used to load the model instance associated with the row (if there is one).
**kwargs – See below.
dry_run (boolean
) – A True value means that no data should be persisted.
use_transactions (boolean
) – A True value means that transactions will be rolled back.
row_number (int
) – The index of the row being imported.
Initializes an object. Implemented in import_export.resources.ModelResource.init_instance()
.
Takes care of saving the object to the database.
Objects can be created in bulk if use_bulk
is enabled.
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.
row – A dict representing the import row.
**kwargs – See :meth:`import_row
Saves m2m fields.
Model instance need to have a primary key value before a many-to-many relationship can be used.
instance – A new or existing model instance.
row – A dict
containing key / value data for the row to be imported.
**kwargs – See import_row()
Returns True
if row
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 overriding
class 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)
instance – A new or updated model instance.
original – The original persisted model instance.
row – A dict
containing key / value data for the row to be imported.
import_validation_errors – A dict
containing key / value data for any identified validation errors.
Takes any validation errors that were raised by import_instance()
, 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_instance()
are re-raised.
ModelResource is Resource subclass for handling Django models.
An optional dict of kwargs. Subclasses can use kwargs to pass dynamic values to enhance import / exports.
alias of Field
Reset the SQL sequences after new objects are imported
Returns a Resource Field instance for the given Django model field.
Prepare widget for fk and o2o fields
Prepare widget for m2m field
Returns a queryset of all objects for this model. Override this if you want to limit the returned queryset.
Initializes a new Django model.
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.
Returns widget kwargs for given field_name.
The inner Meta class allows for class-level configuration of how the Resource should behave. The following options are available:
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
.
Controls the chunk_size argument of Queryset.iterator or, if prefetch_related is used, the per_page attribute of Paginator.
Controls whether full_clean is called during the import process to identify potential validation errors for each (non skipped) row. The default value is False
.
Controls what introspected fields the Resource should NOT include. A blacklist of fields.
Controls export order for columns.
Controls what introspected fields the Resource should include. A whitelist of fields.
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.
Controls which object fields will be used to identify existing instances.
Controls import order for columns.
Controls which class instance will take care of loading existing objects.
Django Model class or full application label string. It is used to introspect available fields.
Controls if the result reports skipped rows. Default value is True
.
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.
If enabled, then skip_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, meaning that the skip_html_diff
value is ignored.
The default value is 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 in the import confirmation page in the admin site, hence when this value is True
, then changes will not be presented on the confirmation screen.
If the HTML report is not required, then setting this value to True
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
.
Controls if the import should skip unchanged records. If True
, then each existing instance is compared with the instance to be imported, and if there are no changes detected, the row is recorded as skipped, and no database update takes place.
The advantages of enabling this option are:
Avoids unnecessary database operations which can result in performance improvements for large datasets.
Skipped records are recorded in each RowResult
.
Skipped records are clearly visible in the import confirmation page.
For the default skip_unchanged
logic to work, the skip_diff
must also be False
(which is the default):
Default value is False
.
If True, the row instance will be stored in each RowResult
. Enabling this parameter will increase the memory usage during import which should be considered when importing large datasets.
This value will always be set to True
when importing via the Admin UI. This is so that appropriate LogEntry
instances can be created.
If True, each row’s raw data will be stored in each RowResult
. Enabling this parameter will increase the memory usage during import which should be considered when importing large datasets.
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.
If True
, this value 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.
Controls if import should use database transactions. Default value is None
meaning settings.IMPORT_EXPORT_USE_TRANSACTIONS
will be evaluated.
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.
This dictionary defines widget kwargs for fields.
Factory for creating ModelResource
class for given Django model. This factory function creates a ModelResource
class dynamically, with support for custom fields, methods.
model – Django model class
resource_class – Base resource class (default: ModelResource)
meta_options – Meta options dictionary
custom_fields – Dictionary mapping field names to Field object
dehydrate_methods – Dictionary mapping field names to dehydrate method (Callable)
ModelResource class
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4