Skip to main content

Minimal test data seeding for Django — plain Python, no magic.

Project description

django-seedkit

Minimal test data seeding for Django — plain Python, no magic.

Auto-discovery • Idempotent • Atomic transactions • Zero configuration

Features

  • Simple: Seed files are plain Python — just use get_or_create() and imports
  • Auto-discovery: Automatically finds setup_*.py files in your apps' seeds/ directories
  • Idempotent: Safe to run multiple times; uses get_or_create() pattern
  • Atomic: All seeds run in a single database transaction (configurable)
  • Dependency management: Use Python imports to control execution order
  • No magic: No decorators, no registration — just import and run

Installation

pip install django-seedkit

Add to your INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "seedkit",
]

Quick Start

1. Create a seed file

Create a seeds/ directory in your app and add seed files:

# myapp/seeds/setup_users.py
from django.contrib.auth import get_user_model
from seedkit import log

User = get_user_model()

admin, created = User.objects.get_or_create(
    username="admin",
    defaults={
        "email": "admin@example.com",
        "is_staff": True,
        "is_superuser": True,
    }
)

if created:
    admin.set_password("admin")
    admin.save()
    log("Created admin user")

2. Run the seeds

python manage.py seeddata

That's it! Your seed file runs automatically.

Managing Dependencies

Seed files execute when imported. Control the order by importing dependencies:

# myapp/seeds/setup_posts.py
from django.contrib.auth import get_user_model

# Import user seeds first - this ensures users exist before posts
from myapp.seeds.setup_users import admin

from myapp.models import Post
from seedkit import log

post, created = Post.objects.get_or_create(
    slug="welcome",
    defaults={
        "title": "Welcome!",
        "author": admin,
        "content": "Hello world",
    }
)

if created:
    log("Created welcome post")

Python's import caching ensures each seed file runs only once, even if imported multiple times.

Commands

# Run all seeds
python manage.py seeddata

# List available seeds without running
python manage.py seeddata --list

# Run seeds for specific apps only
python manage.py seeddata --apps=myapp,otherapp

# Preview what would run (dry run)
python manage.py seeddata --dry-run

# Run without transaction wrapper
python manage.py seeddata --no-atomic

Configuration

Optional configuration via Django settings:

# settings.py
SEEDKIT = {
    # Directory name in each app (default: "seeds")
    "SEED_DIR": "seeds",

    # File pattern to match (default: "setup_*.py")
    "FILE_PATTERN": "setup_*.py",

    # Wrap all seeds in atomic transaction (default: True)
    "ATOMIC": True,

    # Apps to exclude from discovery
    "EXCLUDE_APPS": ["django.contrib.admin"],
}

Helper Functions

Seedkit provides utility functions for common seeding tasks:

from seedkit import log, make_slug, make_hash_id, placeholder_image

# Log messages during seeding
log("Created user")                    # "  Created user"
log("Warning!", level="warning")       # "  ⚠ Warning!"

# Create URL-safe slugs (handles Finnish, Swedish, German, etc.)
make_slug("Pekka Mäkinen")            # "pekka-makinen"
make_slug("Müller Straße")            # "muller-strasse"

# Generate stable hash IDs
make_hash_id("unique-identifier")      # "a1b2c3d4..."

# Placeholder images for development
placeholder_image("apartment-1", 1600, 1200)
# "https://picsum.photos/seed/apartment-1/1600/1200"

Best Practices

Use get_or_create() for Idempotency

# Good - can run multiple times safely
user, created = User.objects.get_or_create(
    username="admin",
    defaults={"email": "admin@example.com"}
)

# Avoid - will fail on second run
user = User.objects.create(username="admin")

Create Related Objects Conditionally

user, created = User.objects.get_or_create(username="admin", ...)

if created:
    # Only create profile if user is new
    Profile.objects.create(user=user, bio="Admin user")
    log("Created admin with profile")

Export Objects for Dependencies

# myapp/seeds/setup_users.py
admin, _ = User.objects.get_or_create(username="admin", ...)
editor, _ = User.objects.get_or_create(username="editor", ...)

# Now other files can import:
# from myapp.seeds.setup_users import admin, editor

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_seedkit-0.1.0.tar.gz (41.9 kB view details)

Uploaded Source

Built Distribution

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

django_seedkit-0.1.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file django_seedkit-0.1.0.tar.gz.

File metadata

  • Download URL: django_seedkit-0.1.0.tar.gz
  • Upload date:
  • Size: 41.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for django_seedkit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 069fb5cd0278fdc2a6d2b2621f24d6c0c185631bf956e0d5a27c463405e44210
MD5 f137dceca07c7f5e74d0557c044d546f
BLAKE2b-256 eab377d35411ddd808bc6fa445855f7bae87ba6f698c0e3930ef8e336cdb6807

See more details on using hashes here.

File details

Details for the file django_seedkit-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_seedkit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d8a41af823d2ecc8bd1e4672aceacc6d61f84473ca6bed351d1ad80e7618f87f
MD5 7da6a68392a5601633df8cb7dc9313bd
BLAKE2b-256 fd0409952faca0303c1920094f3110f04aa11f09ae50b0c9af3f7e82a25a842a

See more details on using hashes here.

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