Skip to main content

Django Gyro

A declarative system for importing and exporting CSV data with Django models. Django Gyro provides a clean, validation-rich way to map CSV columns to Django model fields with automatic foreign key resolution and intelligent data slicing capabilities.

Features

  • Declarative Import System: Define how CSV data maps to your Django models using simple class-based importers
  • Automatic Foreign Key Resolution: Intelligently resolves relationships between models during import
  • Circular Dependency Handling: Automatically resolves circular relationships (e.g., Customer ↔ CustomerReferral)
  • ID Remapping Strategies: Multiple strategies for handling ID conflicts during imports
  • Data Slicing & Export: Export subsets of your data with complex relationships intact
  • Multi-tenant Support: Built-in support for multi-tenant architectures with tenant-aware remapping
  • PostgreSQL Bulk Loading: High-performance imports using PostgreSQL COPY operations
  • Validation-Rich: Comprehensive validation during import/export operations
  • Progress Tracking: Built-in progress bars for long-running operations

Quick Start

Installation

pip install django-gyro

Basic Usage

1. Define Your Importers

# myapp/importers.py
from django_gyro import Importer
from myapp.models import Tenant, Shop, Customer, Product, Order

class TenantImporter(Importer):
    model = Tenant

    class Columns:
        pass

class ShopImporter(Importer):
    model = Shop

    class Columns:
        tenant = Tenant

class CustomerImporter(Importer):
    model = Customer

    class Columns:
        shop = Shop
        tenant = Tenant

2. Export Data

from django_gyro import DataSlicer, ImportJob

# Define what data to export
tenant = Tenant.objects.filter(id=1)
shops = Shop.objects.filter(tenant=tenant)
customers = Customer.objects.filter(shop__in=shops)

# Export to CSV files
DataSlicer.run(
    source=DataSlicer.Postgres(database_url),
    target=DataSlicer.File('/path/to/export/'),
    jobs=[
      ImportJob(model=Tenant, query=tenant),
      ImportJob(model=Shop, query=shops),
      ImportJob(model=Customer, query=customers),
   ],
)

3. Import Data

# Import from CSV files
DataSlicer.run(
    source=DataSlicer.File('/path/to/import/'),
    target=DataSlicer.Postgres(database_url),
    jobs=[
      ImportJob(model=Tenant),
      ImportJob(model=Shop),
      ImportJob(model=Customer),
    ],
)

4. Management Command for CSV Import

Django Gyro includes a management command for importing CSV data:

# Import CSV data with automatic ID remapping
python manage.py import_csv_data --source-dir /path/to/csv/files/

# Preview import without making changes
python manage.py import_csv_data --source-dir /path/to/csv/files/ --dry-run

# Use INSERT statements instead of PostgreSQL COPY (slower but more compatible)
python manage.py import_csv_data --source-dir /path/to/csv/files/ --use-insert

The command automatically handles dependency ordering and uses sequential ID remapping to avoid conflicts.

5. ID Remapping Strategies (Python API)

For more control over ID remapping, use the Python API:

from django_gyro.importing import (
    ImportContext,
    SequentialRemappingStrategy,
    HashBasedRemappingStrategy,
    TenantAwareRemappingStrategy,
    NoRemappingStrategy
)

# Sequential remapping (assigns new IDs starting from MAX+1)
strategy = SequentialRemappingStrategy(model=Customer)
context = ImportContext(
    source_directory=Path("/path/to/csv/files"),
    id_remapping_strategy=strategy
)

# Hash-based remapping (stable IDs using business keys)
strategy = HashBasedRemappingStrategy(
    model=Customer,
    business_key="email"  # Use email to generate stable IDs
)

# Tenant-aware remapping (works with any "tenant" model name)
strategy = TenantAwareRemappingStrategy(
    tenant_model=Organization,  # Your tenant model (any name)
    tenant_mappings={1060: 10, 2000: 11}  # staging -> local IDs
)

# Manual ID mappings
id_mappings = {
    "myapp.Customer": {100: 200, 101: 201},  # old_id: new_id
    "myapp.Order": {500: 600, 501: 601}
}

Use Cases

  • Data Migration: Move data between environments while preserving relationships
  • Selective Exports: Export specific subsets of data for development or testing
  • Multi-tenant Data Management: Handle complex tenant-based data relationships
  • CSV Import/Export: Robust CSV handling with validation and error reporting

Documentation

For detailed documentation, examples, and advanced usage, see TECHNICAL_DESIGN.md.

Requirements

  • Python 3.8+
  • Django 3.2+
  • PostgreSQL (for DataSlicer operations)

Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

Metadata

Release files for django-gyro 0.3.4

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-gyro 0.3.4
File Size Uploaded Provenance Actions
django_gyro-0.3.4.tar.gz 30.9 kB Without provenance Details for django_gyro-0.3.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-gyro 0.3.4
File Interpreter ABI Platform Actions
django_gyro-0.3.4-py3-none-any.whl Python 3 none any Details for django_gyro-0.3.4-py3-none-any.whl

Total release size: 62.9 kB

Release files / django_gyro-0.3.4.tar.gz

Download URL django_gyro-0.3.4.tar.gz
Size 30.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c5cd84381bc8131a20cf14a1b5fc0ffb771661e514d31b425eae29181e8fbac7
BLAKE2b-256 checksum
How to use checksums
3e88fb752565b9ff5163ece3ef116a28b22a57dec5c6e945ec2bb1c7bc9b9a6e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.0 CPython/3.10.18 Linux/6.11.0-1018-azure

Release files / django_gyro-0.3.4-py3-none-any.whl

Download URL django_gyro-0.3.4-py3-none-any.whl
Size 32.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d597085ddb0cbbb85f8b2fb0905900314a7061c4a5ce7950de39176759b071c
BLAKE2b-256 checksum
How to use checksums
18e2667de9f907140350e8b89c8e91544c500474178811f22fdb53f274e09cd2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.0 CPython/3.10.18 Linux/6.11.0-1018-azure

Security for django-gyro 0.3.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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