A Django utility for importing and exporting model data.
Project description
django-dataio
django-dataio is a Django utility package for data import and export.
It allows you to easily export your Django model data into Excel, CSV, JSON, etc., and import data from these formats back into your models.
Installation
pip install django-dataio
Features
- Export Django models using configurable fields.
- Import data into Django models from Excel (and more formats in the future).
- Extensible architecture: add your own exporters/importers.
How to Use
django-dataio provides a mixin DjangoDataIOModelMixin that allows your Django models to export and import data based on configurable fields (ModelField).
You simply inherit the mixin in your model and define ModelField entries for the fields you want to manage.
1. Add DjangoDataIOModelMixin to your model
from django.db import models
from django_dataio.mixins import DjangoDataIOModelMixin
class Customer(DjangoDataIOModelMixin, models.Model): # -> Here!
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField(unique=True)
2. Register fields in ModelField
from django.contrib.contenttypes.models import ContentType
from django_dataio.models import ModelField
from my_app.models import Customer
customer_type = ContentType.objects.get_for_model(Customer)
ModelField.objects.create(name="first_name", model=customer_type, xp=1)
ModelField.objects.create(name="last_name", model=customer_type, xp=2)
ModelField.objects.create(name="email", model=customer_type, xp=3)
# xp is the position/order of the field in export/import files.
3. Export data
from pathlib import Path
from my_app.models import Customer
# Export all customers to Excel
file_path: Path = Customer.data_export('excel')
print(f"Exported file: {file_path}")
4. Import data
from pathlib import Path
from my_app.models import Customer
file_to_import = Path("/tmp/customers.xlsx")
# Import data back into the model
imported_count = Customer.data_import(file_to_import, import_name='excel')
print(f"{imported_count} records imported")
You can add custom exporters or importers by subclassing the base classes:
from django_dataio.handlers.imports import BaseImporter
from django_dataio.handlers.exports import BaseExporter
class CSVExporter(BaseExporter):
...
class CSVImporter(BaseImporter):
...
Check the Export | Import handlers implementation for details.
TODO / Update
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_dataio-0.0.1.tar.gz.
File metadata
- Download URL: django_dataio-0.0.1.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9dd77fdb8016d2a4e938ee30e6f280dab9bf72b192a87bea34eeec2160864522
|
|
| MD5 |
6541e32b244e5c306d514e545ee5a5ca
|
|
| BLAKE2b-256 |
861518bf08b1d2b15f9839497789864aaa019c6ce69a045eaffc29ada3ad90d9
|
File details
Details for the file django_dataio-0.0.1-py3-none-any.whl.
File metadata
- Download URL: django_dataio-0.0.1-py3-none-any.whl
- Upload date:
- Size: 14.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7a03f11dee4ba94dafad0866da7899616eaf7597f075d761069ac9ed50bd03aa
|
|
| MD5 |
50462967cbe2cfc3b2a46bd4363889ce
|
|
| BLAKE2b-256 |
3a5441c8269c7a2338680d8b2343858757ac9b50b6af6157ec4173121e384bdd
|