Skip to main content

Django package for seeding data with nested relationships via YAML

Project description

Django Nested Seed

A Django package for loading seed data from YAML files with support for nested relationships.

Installation

pip install django-nested-seed

Add to INSTALLED_APPS:

INSTALLED_APPS = [
    'django_nested_seed',
]

Quick Start

Create data.yaml:

testapp:
  Author:
    - $ref: alice
      user:
        username: "alice"
        email: "alice@example.com"
      pen_name: "Alice Smith"
      bio: "Software engineer"

  Book:
    - title: "Python Patterns"
      author: "$alice"
      publisher:
        name: "Tech Books Inc"
        country: "UK"
      status: "PUBLISHED"

Load it:

python manage.py nested_seed data.yaml --verbose

More Examples

See the examples/ directory for complete examples of each feature:

YAML Structure

# All models use list format with Django app_label and ModelName
app_label:
  ModelName:
    # Basic object without reference
    - field_name: value
      other_field: value

    # Object with explicit reference key (for later use)
    - $ref: my_key      # Reference key for this object
      field_name: value

    # Auto-generated key (modelname_0, modelname_1, etc.)
    - field_name: value

    # ForeignKey - inline nested object
    - field_name: value
      related_field:
        nested_field: value

    # ForeignKey - reference to existing object
    - field_name: value
      related_field: "$my_key"    # Reference using $ref_key

    # OneToOne - nested directly under parent
    - field_name: value
      one_to_one_field:
        nested_field: value

    # Reverse ForeignKey - nested collection
    - field_name: value
      reverse_relation_set:       # Django reverse accessor
        - child_field: value
        - child_field: value

    # ManyToMany - list of references
    - field_name: value
      many_to_many_field:
        - "$ref_key_1"
        - "$ref_key_2"

    # ManyToMany - mixed references and inline objects
    - field_name: value
      many_to_many_field:
        - "$ref_key"              # Reference existing object
        - inline_field: value     # Create new object inline

    # ManyToMany with through model (extra fields)
    - field_name: value
      many_to_many_field:
        - related_object: "$ref_key"
          extra_field: value      # Through model field
          date_field: "2024-01-01"

# Reference keys must be unique across all models

Features

  • Zero configuration
  • Supports OneToOne, ForeignKey, and ManyToMany relationships
  • ManyToMany with custom through models (extra fields on intermediate table)
  • Mixed relation references and inline definitions
  • Transaction safety with automatic rollback on errors
  • Multiple files can be loaded together
  • Topological sorting handles dependencies automatically

Configuration

Custom Reference Key

Change the reference key field name in settings.py:

NESTED_SEED_CONFIG = {
    'reference_key': 'rid',  # Use 'rid' instead of '$ref'
}

Then use it in your YAML:

app:
  Category:
    - rid: python  # Using custom reference key
      name: "Python"

Development

uv sync
uv run pytest

Requirements

  • Python 3.10+
  • Django 4.2+
  • PyYAML 6.0+

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_nested_seed-0.1.1.tar.gz (66.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_nested_seed-0.1.1-py3-none-any.whl (28.1 kB view details)

Uploaded Python 3

File details

Details for the file django_nested_seed-0.1.1.tar.gz.

File metadata

  • Download URL: django_nested_seed-0.1.1.tar.gz
  • Upload date:
  • Size: 66.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_nested_seed-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e39839f0a405ff2b351d4ba93ce934427e20f03d7309d57e9275620a6c7e8491
MD5 42887868e8e9ac50498226676cb4fd7d
BLAKE2b-256 97127252b91fc460deae272c8d38b0f84abc86b512061223ffe84dff0d4e3fe6

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_nested_seed-0.1.1.tar.gz:

Publisher: publish.yml on omaraboumrad/django-nested-seed

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_nested_seed-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_nested_seed-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 54b7203793a5aa1622aab0d4615e76a22100f1fdde8bd4d75747fc29248722a1
MD5 455b639d4a344e89c467f7ed3eb48efb
BLAKE2b-256 8dd6de40c9535ffba52c0377232cd187ba5e910e97500a24f6c7e80a74821454

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_nested_seed-0.1.1-py3-none-any.whl:

Publisher: publish.yml on omaraboumrad/django-nested-seed

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page