Skip to content

Django Import Export Admin Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Django Import Export Admin Fix. We cover key concepts, practical examples, and best practices.

The Problem

Adding import/export buttons to Django admin requires custom admin views. django-import-export provides ImportExportModelAdmin mixin for instant integration.

Quick Fix

Wrong — custom admin import view

from django.contrib import admin
from django.shortcuts import render
import csv

class ProductAdmin(admin.ModelAdmin):
    def import_csv(self, request):
        if request.method == 'POST':
            reader = csv.DictReader(request.FILES['file'])
            for row in reader:
                Product.objects.create(**row)
        return render(request, 'admin/import.html')

Output: Custom view with no validation, no error reporting, no preview, and security risks with **row.

Correct — ImportExportModelAdmin

from django.contrib import admin
from import_export.admin import ImportExportModelAdmin
from .models import Product
from .resources import ProductResource

@admin.register(Product)
class ProductAdmin(ImportExportModelAdmin):
    resource_class = ProductResource
    list_display = ['name', 'price', 'category', 'is_active']
    list_filter = ['category', 'is_active']
    search_fields = ['name']

Output: Import and export buttons in the admin changelist view. Clicking import shows a file upload form with preview.

Customizing import/export templates

class ProductAdmin(ImportExportModelAdmin):
    resource_class = ProductResource
    import_template_name = 'admin/import_products.html'
    export_template_name = 'admin/export_products.html'

Per-user permissions

from import_export.admin import ImportExportModelAdmin

class ProductAdmin(ImportExportModelAdmin):
    resource_class = ProductResource

    def has_import_permission(self, request):
        return request.user.is_superuser

    def has_export_permission(self, request):
        return request.user.is_staff

Custom actions in admin

from import_export.admin import ImportExportActionModelAdmin

@admin.register(Product)
class ProductAdmin(ImportExportActionModelAdmin):
    resource_class = ProductResource

Output: Import/export available as admin actions (checkbox-based) in addition to the top-level buttons.

Multiple export formats

from import_export.formats.base_formats import CSV, XLSX, JSON

class ProductAdmin(ImportExportModelAdmin):
    resource_class = ProductResource
    formats = [CSV, XLSX, JSON]

Output: Dropdown to choose export format: CSV, Excel, or JSON.

Confirmation page customization

class ProductAdmin(ImportExportModelAdmin):
    resource_class = ProductResource
    show_change_form_import = True  # Show import on change form
    list_per_page = 50

Prevention

  • Use ImportExportModelAdmin instead of custom admin import views.
  • Set resource_class to control which fields are imported/exported.
  • Restrict import permission to superusers with has_import_permission.

Common Mistakes with import export admin

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' causing stack overflow on large lists

These mistakes appear frequently in real-world DJANGO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### How do I add import/export to only some models?

Register those model admins with ImportExportModelAdmin. Other models keep default admin.

Can I customize the import confirmation page?

Yes. Override confirm_import_template or provide a custom template at the admin template path.

Does import/export work with inlines?

Not directly. Import/export works at the model level. For related inlines, use a resource with FK fields.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro