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
  • 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
  • Every save() is guarded against a stale phase, so it can't silently undo a concurrent transition
  • 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 or one inherited from a concrete parent model
  • PhaseGuardError gives refused actions a stable code for machines and a detail for humans
  • Overrides no private Django method
from django.db import models
from django.utils import timezone

from phase.fields import PhaseField
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)


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 ambient-package-update. 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 "ambient-package-update".

Metadata

Release files for django-phase 0.4.1

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.4.1
File Size Uploaded Provenance Actions
django_phase-0.4.1.tar.gz 46.2 kB With provenance Details for django_phase-0.4.1.tar.gz

Built distribution (wheel)

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

Total release size: 61.5 kB

Release files / django_phase-0.4.1.tar.gz

Download URL django_phase-0.4.1.tar.gz
Size 46.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c0c4be1bb529d77df26fcf0fd11880ad611aeafa9204cad34a92847fab20761f
BLAKE2b-256 checksum
How to use checksums
15e1f8246b7a9746a8111f38a238203c231944931b2a4e72f4d622954438424f
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 17, 2026.

Transparency log

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

Download URL django_phase-0.4.1-py3-none-any.whl
Size 15.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9ee1c907de4c20111977877100788db4f5fc0021175a88b11c90de9f393c20d2
BLAKE2b-256 checksum
How to use checksums
a7fe66d7b8a6bbdd2591822b1e4e859d2f95165e234c545be643ac7f9d66fd37
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 17, 2026.

Transparency log

Security for django-phase 0.4.1

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.4.1.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 17, 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