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:
- Basic models - Simple objects with primitive fields
- ForeignKey (Nested) - Create related objects inline
- ForeignKey (Referenced) - Reference objects using
$ref_key - ForeignKey (Mixed) - Combine nested and referenced approaches
- Reverse ForeignKey - Nest child collections under parent
- OneToOne relationships - Nested one-to-one fields
- ManyToMany - M2M with references
- ManyToMany (Through) - M2M with custom through models
- Mixed ManyToMany - M2M with both references and inline objects
- Complex Multi-Level - Comprehensive example combining all features
- Database Lookups - Reference existing database records using
@lookup - Mixed References and Lookups - Combine
$refand@lookup
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 (defined in YAML)
- field_name: value
related_field: "$my_key" # Reference using $ref_key
# ForeignKey - reference to existing database record
- field_name: value
related_field: "@pk:123" # Lookup by primary key
- field_name: value
related_field: "@username:alice" # Lookup by field
- field_name: value
related_field: "@{name:John,email:john@example.com}" # Lookup by multiple fields
# 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" # Reference YAML object
- "$ref_key_2"
# ManyToMany - with database lookups
- field_name: value
many_to_many_field:
- "@slug:python" # Reference existing database record
- "@pk:42"
# ManyToMany - mixed references, lookups, and inline objects
- field_name: value
many_to_many_field:
- "$ref_key" # Reference YAML object
- "@slug:existing" # Reference database record
- 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
- Database lookups - Reference existing database records
- Transaction safety with automatic rollback on errors
- Multiple files can be loaded together
- Topological sorting handles dependencies automatically
Database Lookups
Reference existing database records using @lookup syntax instead of creating new ones:
Lookup by primary key:
author: "@pk:123" # Lookup by primary key
Lookup by single field:
author: "@username:alice" # Lookup by unique field
category: "@slug:python" # Lookup by slug
Lookup by multiple fields:
publisher: "@{name:O'Reilly Media,country:USA}" # Lookup by multiple fields
With Django's related field syntax:
author: "@user__username:alice" # Lookup Author where user.username='alice'
In ManyToMany fields:
Book:
- title: "Django Book"
categories:
- "$new_category" # Reference YAML object
- "@slug:existing" # Reference database record
Features:
- Results are cached to avoid redundant queries
- Clear error messages if record not found
- Works with ForeignKey, OneToOne, and ManyToMany fields
- Can be mixed with
$refreferences in the same file
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
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_nested_seed-0.1.2.tar.gz.
File metadata
- Download URL: django_nested_seed-0.1.2.tar.gz
- Upload date:
- Size: 71.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b53c7e35847fee24e549c92da005d19661d707478ae171bf14c7b10a08f5e6d0
|
|
| MD5 |
eab55eca78338df2b6f7114c32e5ef57
|
|
| BLAKE2b-256 |
ae427e33a77c533c596b52a9e618e24e96c714b07a9d608fc89bbf3f3544dc89
|
Provenance
The following attestation bundles were made for django_nested_seed-0.1.2.tar.gz:
Publisher:
publish.yml on omaraboumrad/django-nested-seed
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_nested_seed-0.1.2.tar.gz -
Subject digest:
b53c7e35847fee24e549c92da005d19661d707478ae171bf14c7b10a08f5e6d0 - Sigstore transparency entry: 757297302
- Sigstore integration time:
-
Permalink:
omaraboumrad/django-nested-seed@41d46c9fbf0a8de482c8785a09e85dd003c9051a -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/omaraboumrad
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@41d46c9fbf0a8de482c8785a09e85dd003c9051a -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_nested_seed-0.1.2-py3-none-any.whl.
File metadata
- Download URL: django_nested_seed-0.1.2-py3-none-any.whl
- Upload date:
- Size: 30.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09e6f95e90da2cf5d04f8f0d497750bf3db9eb7702e563c7ee11bd33afcb0ea2
|
|
| MD5 |
104c6b1cf97420f129a57dc97a8fcddf
|
|
| BLAKE2b-256 |
6c0e64da8b85c2ed2c8bad09a68c2228a46a741d74454ab072ba1e66bf0f0089
|
Provenance
The following attestation bundles were made for django_nested_seed-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on omaraboumrad/django-nested-seed
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_nested_seed-0.1.2-py3-none-any.whl -
Subject digest:
09e6f95e90da2cf5d04f8f0d497750bf3db9eb7702e563c7ee11bd33afcb0ea2 - Sigstore transparency entry: 757297310
- Sigstore integration time:
-
Permalink:
omaraboumrad/django-nested-seed@41d46c9fbf0a8de482c8785a09e85dd003c9051a -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/omaraboumrad
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@41d46c9fbf0a8de482c8785a09e85dd003c9051a -
Trigger Event:
release
-
Statement type: