Django application and library for importing and exporting data with included admin integration.
npx @tessl/cli install tessl/pypi-django-import-export@4.3.0Django Import Export is a Django application and library for importing and exporting data with included admin integration. It enables developers to handle import/export operations from/to various file formats (CSV, XLSX, JSON, YAML, HTML, pandas) through both programmatic APIs and Django Admin UI integration.
pip install django-import-exportfrom import_export import resources, fields, widgets
from import_export.admin import ImportExportModelAdmin, ExportActionModelAdmin
from import_export import results, exceptionsFor format handling:
from import_export.formats.base_formats import CSV, XLSX, JSON, YAML, TSVFor advanced usage:
from import_export.instance_loaders import CachedInstanceLoader
from import_export.signals import post_import, post_export
from import_export.forms import ImportForm, ExportFormfrom django.db import models
from import_export import resources, fields, widgets
from import_export.admin import ImportExportModelAdmin
# Define a model
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=100)
published_date = models.DateField()
isbn = models.CharField(max_length=13)
# Define a resource for the model
class BookResource(resources.ModelResource):
title = fields.Field(attribute='title', column_name='Book Title')
author = fields.Field(attribute='author', column_name='Author Name')
published_date = fields.Field(
attribute='published_date',
column_name='Publication Date',
widget=widgets.DateWidget(format='%Y-%m-%d')
)
isbn = fields.Field(attribute='isbn', column_name='ISBN')
class Meta:
model = Book
fields = ('title', 'author', 'published_date', 'isbn')
export_order = ('title', 'author', 'published_date', 'isbn')
# Use in Django admin
class BookAdmin(ImportExportModelAdmin):
resource_class = BookResource
list_display = ['title', 'author', 'published_date']
# Programmatic usage
resource = BookResource()
# Export data
dataset = resource.export()
csv_data = dataset.csv
# Import data from CSV
from tablib import Dataset
dataset = Dataset()
dataset.csv = csv_data
result = resource.import_data(dataset, dry_run=True)
if not result.has_errors():
resource.import_data(dataset, dry_run=False)Django Import Export is built around several key components that work together to provide flexible data import/export capabilities:
The declarative approach using Resource classes allows for powerful customization while maintaining simplicity for common use cases.
Core system for defining how Django models map to import/export data, including field mapping, data transformation, and business logic hooks.
class Resource(metaclass=DeclarativeMetaclass):
def __init__(self, **kwargs): ...
def import_data(self, dataset, dry_run=False, raise_errors=False, use_transactions=None, collect_failed_rows=False, rollback_on_validation_errors=False, **kwargs) -> Result: ...
def export(self, queryset=None, **kwargs) -> Dataset: ...
def import_row(self, row, instance_loader, **kwargs) -> RowResult: ...
def get_or_init_instance(self, instance_loader, row) -> tuple: ...
def validate_instance(self, instance, import_validation_errors=None, validate_unique=True): ...
def save_instance(self, instance, is_create, row, **kwargs): ...
def bulk_create(self, using_transactions, dry_run, raise_errors, batch_size=None, result=None): ...
def bulk_update(self, using_transactions, dry_run, raise_errors, batch_size=None, result=None): ...
def bulk_delete(self, using_transactions, dry_run, raise_errors, result=None): ...
# Hook methods for customization
def before_import(self, dataset, **kwargs): ...
def after_import(self, dataset, result, **kwargs): ...
def before_import_row(self, row, **kwargs): ...
def after_import_row(self, row, row_result, **kwargs): ...
class ModelResource(Resource, metaclass=ModelDeclarativeMetaclass):
def get_queryset(self) -> QuerySet: ...
def field_from_django_field(self, field_name, django_field, readonly) -> Field: ...
class Field:
def __init__(self, attribute=None, column_name=None, widget=None, default=NOT_PROVIDED, readonly=False, saves_null_values=True, dehydrate_method=None, m2m_add=False): ...
def clean(self, row, **kwargs): ...
def export(self, instance, **kwargs): ...
def save(self, instance, row, is_m2m=False, **kwargs): ...Complete Django admin integration with import/export functionality, including file upload, validation, confirmation workflows, and error handling.
class ImportMixin(BaseImportMixin, ImportExportMixinBase):
def import_action(self, request, **kwargs): ...
def process_import(self, request, **kwargs): ...
def has_import_permission(self, request): ...
class ExportMixin(BaseExportMixin, ImportExportMixinBase):
def export_action(self, request): ...
def get_export_data(self, file_format, request, queryset, **kwargs): ...
def has_export_permission(self, request): ...
class ImportExportModelAdmin(ImportExportMixin, admin.ModelAdmin): ...
class ExportActionModelAdmin(ExportActionMixin, admin.ModelAdmin): ...Comprehensive widget system for transforming data between Python objects and serialized formats, including support for various data types and relationships.
class Widget:
def clean(self, value, row=None, **kwargs): ...
def render(self, value, obj=None, **kwargs): ...
class ForeignKeyWidget(Widget):
def __init__(self, model, field="pk", use_natural_foreign_keys=False, key_is_id=False, **kwargs): ...
def get_queryset(self, value, row, *args, **kwargs): ...
class ManyToManyWidget(Widget):
def __init__(self, model, separator=None, field=None, **kwargs): ...
# Data type widgets
class DateWidget(Widget): ...
class DateTimeWidget(Widget): ...
class BooleanWidget(Widget): ...
class JSONWidget(Widget): ...Widgets and Data Transformation
Support for multiple file formats through tablib integration, including CSV, XLSX, JSON, YAML, and more, with configurable format options.
class Format:
def create_dataset(self, in_stream): ...
def export_data(self, dataset, **kwargs): ...
def is_binary(self): ...
def get_extension(self): ...
class CSV(TextFormat): ...
class XLSX(TablibFormat): ...
class JSON(TextFormat): ...
class YAML(TextFormat): ...
# Format utilities
def get_format_class(format_name, file_name, encoding=None): ...
def get_default_format_names(): ...Form classes for handling file uploads, format selection, field selection, and import confirmation workflows in web interfaces.
class ImportForm(ImportExportFormBase):
def __init__(self, import_formats, **kwargs): ...
class ConfirmImportForm(forms.Form):
def __init__(self, confirm_form_class, import_form_data, **kwargs): ...
class ExportForm(ImportExportFormBase): ...
class SelectableFieldsExportForm(ExportForm): ...Django management commands for command-line import and export operations, supporting various formats and configuration options.
# Export command
python manage.py export <format> <resource> [--encoding=<encoding>]
# Import command
python manage.py import <format> <resource> <file> [--encoding=<encoding>] [--dry-run]Django Import Export provides comprehensive error handling and result tracking through specialized exception classes and result containers.
class ImportExportError(Exception): ...
class FieldError(ImportExportError): ...
class ImportError(ImportExportError):
def __init__(self, error, number=None, row=None): ...
class Result:
def has_errors(self): ...
def has_validation_errors(self): ...
def valid_rows(self): ...
class RowResult:
IMPORT_TYPE_UPDATE = 'update'
IMPORT_TYPE_NEW = 'new'
IMPORT_TYPE_DELETE = 'delete'
IMPORT_TYPE_SKIP = 'skip'
IMPORT_TYPE_ERROR = 'error'
IMPORT_TYPE_INVALID = 'invalid'
def is_update(self): ...
def is_new(self): ...
def is_error(self): ...Signal support for extending import/export workflows with custom processing logic.
from import_export.signals import post_import, post_export
# Django signals
post_import = Signal() # Args: model
post_export = Signal() # Args: modelPerformance optimization for loading existing instances during import operations.
class BaseInstanceLoader:
def get_instance(self, row): ...
class ModelInstanceLoader(BaseInstanceLoader):
def __init__(self, resource_class, dataset=None): ...
class CachedInstanceLoader(ModelInstanceLoader):
# Pre-loads instances for performance
def __init__(self, resource_class, dataset=None): ...Resource configuration through Meta class options.
class Meta:
model = None # Django model class
fields = None # Field whitelist
exclude = None # Field blacklist
import_id_fields = ('id',) # Instance identification fields
import_order = () # Import field order
export_order = () # Export field order
use_transactions = None # Transaction usage
skip_unchanged = False # Skip unchanged records
clean_model_instances = False # Call full_clean()
use_bulk = False # Enable bulk operations
batch_size = 1000 # Bulk operation batch sizefrom typing import Dict, List, Optional, Union, Any, Tuple
from django.db.models import Model, QuerySet
from django.core.exceptions import ValidationError
from tablib import Dataset
# Core types
ResourceOptions = Dict[str, Any]
WidgetKwargs = Dict[str, Any]
RowData = Dict[str, Any]
ImportResult = Result
ExportDataset = Dataset
InstanceTuple = Tuple[Model, bool] # (instance, created)
ErrorList = List[Union[Exception, ValidationError]]
# Metaclasses and constants
DeclarativeMetaclass = type # Metaclass for declarative resource definition
ModelDeclarativeMetaclass = type # Metaclass for model-based resources
NOT_PROVIDED = object() # Sentinel value for unset field defaults
# Result classes
class Result:
rows: List[RowResult]
invalid_rows: List[dict]
error_rows: List[dict]
totals: Dict[str, int]
class RowResult:
IMPORT_TYPE_NEW = 'new'
IMPORT_TYPE_UPDATE = 'update'
IMPORT_TYPE_DELETE = 'delete'
IMPORT_TYPE_SKIP = 'skip'
IMPORT_TYPE_ERROR = 'error'
IMPORT_TYPE_INVALID = 'invalid'