Skip to main content

django-model-import

PyPI version

Django Model Import is a light weight CSV importer built for speed.

It uses a standard Django ModelForm to parse each row, giving you a familiar API to work with for data validation and model instantiation. In most cases, if you already have a ModelForm for the ContentType you are importing you do not need to create an import specific form.

To present feedback to the end-user running the import you can easily generate a preview of the imported data by toggling the commit parameter.

It also provides some import optimized fields for ForeignKey's, allowing preloading all possible values, or caching each lookup as it occurs, or looking up a model where multiple fields are needed to uniquely identify a resource.

Installation

poetry add django-model-import

Quickstart

import djangomodelimport

class BookImporter(djangomodelimport.ImporterModelForm):
    name = forms.CharField()
    author = CachedChoiceField(queryset=Author.objects.all(), to_field='name')

    class Meta:
        model = Book
        fields = (
            'name',
            'author',
        )

with default_storage.open('books.csv', 'rb') as fh:
    data = fh.read().decode("utf-8")

# Use tablib
parser = djangomodelimport.TablibCSVImportParser(BookImporter)
headers, rows = parser.parse(data)

# Process
importer = djangomodelimport.ModelImporter(BookImporter)
preview = importer.process(headers, rows, commit=False)
errors = preview.get_errors()

if errors:
    print(errors)

importresult = importer.process(headers, rows, commit=True)
for result in importresult.get_results():
    print(result.instance)

Composite key lookups

Often a relationship cannot be referenced via a single unique string. For this we can use a CachedChoiceField with a CompositeLookupWidget. The widget looks for the values under the type and variant columns in the source CSV, and does a unique lookup with the field names specified in to_field, e.g. queryset.get(type__name=type, name=variant).

The results of each get are cached internally for the remainder of the import minimising any database access.

class AssetImporter(ImporterModelForm):
    site = djangomodelimport.CachedChoiceField(queryset=Site.objects.active(), to_field='ref')
    type = djangomodelimport.CachedChoiceField(queryset=AssetType.objects.filter(is_active=True), to_field='name')
    type_variant = djangomodelimport.CachedChoiceField(
        queryset=InspectionItemTypeVariant.objects.filter(is_active=True),
        required=False,
        widget=djangomodelimport.CompositeLookupWidget(source=('type', 'variant')),
        to_field=('type__name', 'name'),
    )
    contractor = djangomodelimport.CachedChoiceField(queryset=Contractor.objects.active(), to_field='name')

Flat related fields

Often you'll have a OneToOneField or just a ForeignKey to another model, but you want to be able to create/update that other model via this one. You can flatten all of the related model's fields onto this importer using FlatRelatedField.

class ClientImporter(ImporterModelForm):
    primary_contact = FlatRelatedField(
        queryset=ContactDetails.objects.all(),
        fields={
            'contact_name': {'to_field': 'name', 'required': True},
            'email': {'to_field': 'email'},
            'email_cc': {'to_field': 'email_cc'},
            'mobile': {'to_field': 'mobile'},
            'phone_bh': {'to_field': 'phone_bh'},
            'phone_ah': {'to_field': 'phone_ah'},
            'fax': {'to_field': 'fax'},
        },
    )

    class Meta:
        model = Client
        fields = (
            'name',
            'ref',
            'is_active',
            'account',

            'primary_contact',
        )

Tests

Run tests with python example/manage.py test testapp

Release files for django-model-import 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-model-import 0.9.0
File Size Uploaded
django_model_import-0.9.0.tar.gz 16.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-model-import 0.9.0
File Interpreter ABI Platform
django_model_import-0.9.0-py3-none-any.whl Python 3 none any Details

Total release size:37.3 kB

Release files / django_model_import-0.9.0.tar.gz

Download URL django_model_import-0.9.0.tar.gz
Size 16.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4de3bce73df6305818aa50c8e4c746488f8d5408ba16f76ea9dcdaabcd0eb325
BLAKE2b-256 checksum
How to use checksums
40235e45ff9e134bfb9bf432286e6e5f4754acd708eb7e2f1df867ce60b75366
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.25 {"installer":{"name":"uv","version":"0.11.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / django_model_import-0.9.0-py3-none-any.whl

Download URL django_model_import-0.9.0-py3-none-any.whl
Size 21.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1d54acb6e106e9a9902c5f8365b78ce2dd14109eef56e592b44c1e46e791fca2
BLAKE2b-256 checksum
How to use checksums
44b6e43ad610723738e1e820144ab415eae0cff1b71f4053d5e8f80d1be17f08
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.25 {"installer":{"name":"uv","version":"0.11.25","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.9.0 This release

2 release files

0.8.0

2 release files

0.7.7

2 release files

0.7.6

2 release files

0.7.5

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.18

2 release files

0.4.17

2 release files

0.4.16

2 release files

0.4.15

2 release files

0.4.14

2 release files

0.4.4

1 release file

0.4.3

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3

1 release file

0.2.1

1 release file

0.2

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page