Skip to main content

django-versioned-models

PyPI version Python Development Status Maintenance PyPI License


Drop-in versioning for Django models. Every model that inherits from VersionedModel gets full release management, a data status workflow, and CI integration — automatically.


🚀 Features

  • Release management — every row in every table is tagged to a release. Branch, lock, patch, and deprecate with simple commands.
  • Data status workflow — DRAFT → FUTURE_DEVELOPMENT / FUTURE_DEPRECATION → APPROVED. CI only sees approved rows. Live architect edits never break tests.
  • Lock enforcement — locked and deployed releases are fully immutable at the model and QuerySet level. No edits, inserts, approvals, or deletes — from Admin, API, shell, or bulk operations.
  • Soft-delete — rows can be deactivated without deletion. Deactivating an approved row resets it to DRAFT so it must be re-approved before CI sees it again.
  • Auto-discovery — inherit VersionedModel and your model is versioned. No registration needed.
  • Topological FK sort — models are duplicated in the correct dependency order automatically when branching a release.
  • Soft deprecation — old releases are hidden by default but data is always preserved and reversible.
  • CI-ready commands — create_release, approve_release, lock_release, and more, ready to plug into any pipeline.

📦 Installation

pip install django-versioned-models

⚡ Quick Start

1. Add to INSTALLED_APPS

INSTALLED_APPS = [
    ...
    "django_versioned_models",
]

2. Run migrations

python manage.py migrate

3. Define your versioned models

from django.db import models
from django_versioned_models.mixins import VersionedModel

class Product(VersionedModel):
    name = models.CharField(max_length=255)
    price = models.DecimalField(max_digits=10, decimal_places=2)

    class Meta(VersionedModel.Meta):
        unique_together = [("release", "name")]  # unique per release, not globally

Important: always inherit Meta from VersionedModel.Meta to preserve the composite index on (release, status, active).

4. Run migrations for your models

python manage.py makemigrations
python manage.py migrate

5. Create the first release

python manage.py create_release --release-version v1.0.0

6. Add data, then lock

# Add data via Admin, API, or shell — all rows start as DRAFT

python manage.py lock_release --release-version v1.0.0

🔄 Ongoing Workflow

create_release → architects edit (DRAFT) → approve_release → lock_release → deploy_release → ship
# Branch from the previous locked release
python manage.py create_release --release-version v1.1.0 --based-on v1.0.0

# Architects add and edit rows (status=DRAFT by default)

# Need a correction? Unlock is allowed before deployment
python manage.py unlock_release --release-version v1.1.0          # interactive
python manage.py unlock_release --release-version v1.1.0 --force  # CI / automation

# CI approves all stable DRAFT rows (FUTURE_DEVELOPMENT / FUTURE_DEPRECATION rows are left untouched)
# Must run before lock_release — locked releases reject approval
python manage.py approve_release --release-version v1.1.0

# Run tests against approved data only
pytest

# Lock — no more edits (including approvals) after this point, then deploy
python manage.py lock_release --release-version v1.1.0
python manage.py deploy_release --release-version v1.1.0  # requires locked — permanently blocks unlock

# Bug found after deployment? Create a patch — never unlock a deployed release
python manage.py create_release --release-version v1.1.1 --based-on v1.1.0

🔍 How It Works

Every model that inherits from VersionedModel automatically gets:

Field Type Description
release FK → Release Which release this row belongs to
status CharField Data readiness: draft / future_development / future_deprecation / approved
active BooleanField Soft-delete flag. Inactive rows are invisible to for_release() and approved()

Release state fields:

Field Type Description
is_locked BooleanField Immutable — no edits or deletes allowed
deployed BooleanField Deployed to production — unlock is permanently blocked
is_deprecated BooleanField Hidden by default, data preserved

Status Flow

DRAFT <-> FUTURE_DEVELOPMENT -> APPROVED   (APPROVED is one-way — CI only)
DRAFT <-> FUTURE_DEPRECATION -> APPROVED   (APPROVED is one-way — CI only)
Status Set by Meaning
DRAFT Architects Being worked on
FUTURE_DEVELOPMENT Architects Planned addition — not ready yet (mark_future_development())
FUTURE_DEPRECATION Architects Planned removal — not ready yet (mark_future_deprecation())
APPROVED CI only Stable — what tests run against

Active / Inactive (Soft-Delete)

row.deactivate()   # sets active=False, resets APPROVED → DRAFT
row.reactivate()   # sets active=True, status stays DRAFT — must re-approve

Deactivating an approved row resets its status to DRAFT, so it must go through the approval flow again before CI can see it.

Lock Enforcement

Locked and deployed releases are immutable. Every write raises ValidationError — whatever the row's status, from Admin, API, or shell:

row.save()     # edit or insert — raises if the row's current or target release is locked
row.delete()   # raises if release is locked
row.approve()  # raises — approve before locking

Product.objects.filter(release=locked).update(...)  # raises
Product.objects.filter(release=locked).delete()     # raises
Product.objects.bulk_create([...])                  # raises if any row targets a locked release
Product.objects.bulk_update([...], fields)          # raises if any row is in / moves to a locked release

Lock state is always read from the database, so a stale Release instance can't bypass the check. Need a correction before deployment? unlock_release, fix, re-approve, lock again. After deployment, create a patch release.


📋 Management Commands

Command Description
create_release --release-version v1.0.0 Create a standalone release (unlocked)
create_release --release-version v1.1.0 --based-on v1.0.0 Branch from a locked release — copies all rows
lock_release --release-version v1.1.0 Lock a release (immutable)
approve_release --release-version v1.1.0 Approve all active DRAFT rows (CI only — FUTURE_DEVELOPMENT, FUTURE_DEPRECATION, and inactive untouched)
deploy_release --release-version v1.1.0 Mark as deployed — permanently blocks unlock
unlock_release --release-version v1.1.0 Unlock with interactive confirmation (pre-deployment only)
unlock_release --release-version v1.1.0 --force Unlock without prompt — safe for CI/automation (pre-deployment only)
rename_release --release-version v1.1.0 --new-version v1.1.0-rc Rename a release (data untouched; blocked for deployed releases)
delete_release --release-version v1.1.0 Permanently delete a release and all its rows, with interactive confirmation (blocked for deployed releases; branches of it are kept but lose their based_on, with a warning)
delete_release --release-version v1.1.0 --dry-run Show what would be deleted, without deleting
delete_release --release-version v1.1.0 --force Delete without prompt — for CI/automation
deprecate_release --release-version v1.0.0 Soft-deprecate (data preserved, hidden by default)
deprecate_release --release-version v1.0.0 --undo Restore a deprecated release

🗄 Querying

from django_versioned_models.models import Release

release = Release.objects.get(version="v1.1.0")

# Active rows only, all statuses — use in architect-facing GUI views
Product.objects.for_release(release)

# Approved + active rows only — use in CI and production logic
Product.objects.approved(release)

# All rows including inactive — use for copy/audit only, not business logic
Product.objects.all_rows(release)

# Filter by status directly
Product.objects.for_release(release).filter(status=DataStatus.FUTURE_DEPRECATION)

📥 Imports Reference

from django_versioned_models.mixins import VersionedModel, DataStatus
from django_versioned_models.models import Release
from django_versioned_models.services import (
    create_release,
    lock_release,
    rename_release,
    delete_release,
    get_versioned_models,
    get_versioned_models_ordered,
)

🧪 Running Tests

uv run pytest

🤝 Contributing

Contributions are welcome! Please fork the repo, create a branch, and submit a pull request.


📄 License

MIT License — see LICENSE for details.

Metadata

Release files for django-versioned-models 3.1.0

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-versioned-models 3.1.0
File Size Uploaded Provenance Actions
django_versioned_models-3.1.0.tar.gz 15.6 kB Without provenance Details for django_versioned_models-3.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-versioned-models 3.1.0
File Interpreter ABI Platform Actions
django_versioned_models-3.1.0-py3-none-any.whl Python 3 none any Details for django_versioned_models-3.1.0-py3-none-any.whl

Total release size: 39.1 kB

Release files / django_versioned_models-3.1.0.tar.gz

Download URL django_versioned_models-3.1.0.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
79f2aad44c38522b2034ae9a4648ac363da41f3f14e07a04372ad0a53ddac45f
BLAKE2b-256 checksum
How to use checksums
70a4fb93491f48a25a60d81610521964af428353e16719ffd271a5a7e014af4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / django_versioned_models-3.1.0-py3-none-any.whl

Download URL django_versioned_models-3.1.0-py3-none-any.whl
Size 23.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
664a4b456089be18540b3c7e55bf469aea742c0b5bf5f39a9fe6ee4e90be2f90
BLAKE2b-256 checksum
How to use checksums
e61817f82b99eb74e61612d5332b4aaf2713e0125e1484e23cc5709d17edfdc8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for django-versioned-models 3.1.0

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