Skip to Content
FeaturesModulesImport / Export

Import/Export

The django_import_export module integrates django-import-export  into Django-CFG, re-exporting its components through the Django-CFG registry. The templates and forms are the ones django-import-export ships; the django-cfg admin theme styles them.

What It Provides

  • Registry integration — import admin classes, forms, and resources from django_cfg directly
  • Stock templates and forms — as django-import-export ships them; the admin theme (cfg-theme.css) styles its rows
  • Zero overhead — direct re-exports of the original library; no behavioural changes
  • Full compatibility — 100% compatible with upstream django-import-export

Installation

django-import-export is a base dependency of django-cfg (>=4.3.0,<5.0) and import_export is in the generated INSTALLED_APPS. No additional configuration is required.

PydanticAdmin is already built on ImportExportModelAdmin, so a PydanticAdmin gets import/export with AdminConfig(import_export_enabled=True): the resource is generated from the config, or pass resource_class.

Quick Start

from django_cfg import ImportExportModelAdmin, BaseResource from django.contrib import admin from .models import Vehicle class VehicleResource(BaseResource): class Meta: model = Vehicle fields = ('id', 'brand', 'model', 'year', 'price', 'mileage') export_order = ('id', 'brand', 'model', 'year', 'price', 'mileage') @admin.register(Vehicle) class VehicleAdmin(ImportExportModelAdmin): resource_class = VehicleResource list_display = ('brand', 'model', 'year', 'price') list_filter = ('brand', 'year') search_fields = ('brand', 'model')

Available Components

All components are importable from django_cfg:

from django_cfg import ( ImportExportMixin, ImportExportModelAdmin, ExportMixin, ImportMixin, BaseResource, ImportForm, ExportForm, SelectableFieldsExportForm, )

Admin Classes

ClassExtendsDescription
ImportExportModelAdminimport_export.admin.ImportExportModelAdminFull admin class with import and export
ImportExportMixinimport_export.admin.ImportExportMixinMixin for adding import/export to an existing admin class
ExportMixinimport_export.admin.ExportMixinMixin for export-only admin
ImportMixinimport_export.admin.ImportMixinMixin for import-only admin

The classes are the upstream ones, with their own changelist templates and form classes.

Forms

FormDescription
ImportFormimport_export.forms.ImportForm
ExportFormimport_export.forms.ExportForm
SelectableFieldsExportFormimport_export.forms.SelectableFieldsExportForm, export with field selection

Resources

BaseResource is an alias for import_export.resources.ModelResource. Use it to define which model fields to include in import/export operations:

from django_cfg import BaseResource from import_export import fields, widgets class ProductResource(BaseResource): category_name = fields.Field( column_name='category', attribute='category__name', widget=widgets.ForeignKeyWidget(Category, 'name') ) class Meta: model = Product fields = ('id', 'name', 'category_name', 'price', 'quantity') export_order = ('id', 'name', 'category_name', 'price', 'quantity')

Using Only Export or Only Import

from django_cfg import ExportMixin, ImportMixin, BaseResource from django.contrib import admin # Export-only admin class ReportAdmin(ExportMixin, admin.ModelAdmin): resource_class = ReportResource # Import-only admin class UserImportAdmin(ImportMixin, admin.ModelAdmin): resource_class = UserResource

Custom Import Form

from django_cfg import ImportForm, ImportExportModelAdmin from django import forms class CustomImportForm(ImportForm): update_existing = forms.BooleanField( required=False, initial=True, help_text='Update existing records if key matches' ) class ProductAdmin(ImportExportModelAdmin): import_form_class = CustomImportForm

Custom Export Form

from django_cfg import ExportForm, ImportExportModelAdmin class CustomExportForm(ExportForm): # Add custom fields to the export form pass class ProductAdmin(ImportExportModelAdmin): export_form_class = CustomExportForm

Resource Hooks

BaseResource supports the full set of django-import-export lifecycle hooks:

class ProductResource(BaseResource): class Meta: model = Product fields = ('name', 'price', 'sku') def before_import(self, dataset, **kwargs): """Pre-process the entire dataset.""" required = ['name', 'price'] missing = [col for col in required if col not in dataset.headers] if missing: raise ValueError(f"Missing required columns: {missing}") def before_import_row(self, row, **kwargs): """Modify a single row before import.""" if 'price' in row: row['price'] = float(str(row['price']).replace(',', '.')) def skip_row(self, instance, original, row, import_validation_errors=None): """Skip rows that fail custom validation.""" if hasattr(instance, 'price') and instance.price <= 0: return True return super().skip_row(instance, original, row, import_validation_errors) def after_import(self, dataset, result, **kwargs): """Post-process after import completes.""" if not result.has_errors(): # trigger notifications or side effects pass def dehydrate_price(self, product): """Custom export formatting for the price field.""" return f"{product.price:.2f}"

Templates

django-cfg ships no import/export templates. The changelist, import and export pages are the ones in import_export/templates/admin/import_export/; to change one, override it in your project’s templates/admin/import_export/ as with any Django admin template.

Changelist Context

With import_export_enabled=True, PydanticAdmin adds the permission flags to the changelist template context:

extra_context['has_import_permission'] = self.has_import_permission(request) extra_context['has_export_permission'] = self.has_export_permission(request)

This allows templates to conditionally show import/export buttons based on user permissions.

Equivalent Upstream Imports

The module exists to provide a single consistent import path. The underlying library is unchanged:

# Instead of: from import_export.admin import ImportExportModelAdmin from import_export.resources import ModelResource from import_export.forms import ImportForm, ExportForm # Use: from django_cfg import ImportExportModelAdmin, BaseResource, ImportForm, ExportForm

Full Documentation

For complete resource, field, and widget documentation, see the official django-import-export documentation .

TAGS: import-export, admin, data-management, csv, xlsx DEPENDS_ON: [django-admin]

Last updated on