Skip to main content

PyPI release Downloads Coverage Linting Coding Style Documentation Status

Race-safe state machines for Django models: a protected PhaseField, transitions as one conditional UPDATE, and guards that tell humans and machines why an action was refused.

PyPI • GitHub • Full documentation

Creator & Maintainer: Beyonder Deutschland

Features

  • Declare the allowed transitions once, as a table on the field
  • Assigning the phase directly raises; only move() and move_many() change it, and force_move() as an explicit escape hatch for data repairs
  • move() writes phase and further fields in one conditional UPDATE, so of two concurrent transitions from the same phase exactly one succeeds
  • move_many() moves a whole queryset from one phase to another with one conditional UPDATE, checked against the same table
  • phase_moved and phase_moved_many tell receivers which phase the rows came from and where they went
  • Every save() is guarded against a stale phase, so it can't silently undo a concurrent transition
  • PhaseManager refuses update(), bulk_update() and bulk_create(update_conflicts=True) on the phase field
  • Phases as strings (PhaseField) or numbers (IntegerPhaseField); migrations see a plain Django field and never import phase
  • System checks catch a missing mixin, a second phase field, a nullable one, one inherited from a concrete parent model or a manager that lets bulk writes past the field
  • PhaseGuardError gives refused actions a stable code for machines and a detail for humans
  • An optional exception handler for Django REST framework answers refused actions with HTTP 409
  • Overrides no private Django method
from django.db import models
from django.utils import timezone

from phase.fields import PhaseField
from phase.managers import PhaseManager
from phase.mixins.phase_guard import PhaseGuardMixin
from phase.transitions import move


class Order(PhaseGuardMixin, models.Model):
    class Status(models.TextChoices):
        OPEN = "open"
        PAID = "paid"
        SHIPPED = "shipped"
        CANCELLED = "cancelled"

    status = PhaseField(
        max_length=10,
        choices=Status.choices,
        default=Status.OPEN,
        transitions={
            Status.OPEN: (Status.PAID, Status.CANCELLED),
            Status.PAID: (Status.SHIPPED,),
        },
    )
    paid_at = models.DateTimeField(null=True, blank=True)

    objects = PhaseManager()


order = Order.objects.create()
move(obj=order, to=Order.Status.PAID, paid_at=timezone.now())

order.status = Order.Status.OPEN  # raises ProtectedPhaseFieldError
move(obj=order, to=Order.Status.OPEN)  # raises IllegalPhaseTransitionError

Installation

  • Install the package via pip:

    pip install django_phase

    or via uv:

    uv add django_phase

  • Add module to INSTALLED_APPS within the main django settings.py:

INSTALLED_APPS = (
    # ...
    "phase",
)

Releasing a new version

Releases are fully automated. Push a version tag and the pipeline will build, sign with Sigstore, publish to PyPI via Trusted Publishing, and create a GitHub Release — no API tokens needed.

git tag v<version>          # e.g. git tag v1.2.3
git push origin v<version>

Tags must start with v. Tags without the prefix won't trigger the pipeline.

First-time setup

Before the pipeline can run for the first time, an admin must:

  1. Create GitHub Environment pypi

    • Go to Settings → Environments → New environment, name it exactly pypi
    • Under Deployment branches and tags, add a tag rule with pattern v*
    • Optionally add required reviewers for a manual approval gate
  2. Configure PyPI Trusted Publisher

    • Go to PyPI → Project settings → Publishing → Add a new publisher
    • Fill in: Owner beyonder-deutschland, Repository django-phase, Workflow release.yml, Environment pypi

Publish to ReadTheDocs.io

  • Fetch the latest changes in GitHub mirror and push them
  • Trigger new build at ReadTheDocs.io (follow instructions in admin panel at RTD) if the GitHub webhook is not yet set up.

Maintenance

Please note that this package supports the package-foundry. So you don't have to worry about the maintenance of this package. This updater is rendering all important configuration and setup files. It works similar to well-known updaters like pyupgrade or django-upgrade.

To run an update, refer to the documentation page of the "package-foundry".

Metadata

Release files for django-phase 0.5.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-phase 0.5.0
File Size Uploaded Provenance Actions
django_phase-0.5.0.tar.gz 58.5 kB With provenance Details for django_phase-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-phase 0.5.0
File Interpreter ABI Platform Actions
django_phase-0.5.0-py3-none-any.whl Python 3 none any Details for django_phase-0.5.0-py3-none-any.whl

Total release size: 79.6 kB

Release files / django_phase-0.5.0.tar.gz

Download URL django_phase-0.5.0.tar.gz
Size 58.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4d8f855b3acb7eefc06fb98e17291d448089e7b37e45c9b574abcb1de6b68f9e
BLAKE2b-256 checksum
How to use checksums
87b870bd7108e296ef70af62483d614a8f0fc5d24d7047f0e7b6c6235f3fa502
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Release files / django_phase-0.5.0-py3-none-any.whl

Download URL django_phase-0.5.0-py3-none-any.whl
Size 21.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
23007ce09fe737e1620412c3c851c2ef2572297d77e96bbfe9aee46ba16b8b98
BLAKE2b-256 checksum
How to use checksums
4ee209feb2bdd22338eb4585a6d16950e24a1fd46c20bcb071f1cc179bb472f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Security for django-phase 0.5.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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for django_phase-0.5.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

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