Skip to main content

Dynamic workflow extensions for django-fsm-2

Project description

Django FSM Dynamic

Dynamic workflow extensions for django-fsm-2 that allow optional Django apps to modify FSM state machines without creating database migrations.

PyPI version Python Support Django Support

Features

  • Dynamic State Enums: Extend state enums at runtime without migrations
  • Callable Choices: Prevent Django from generating migrations when choices change
  • Transition Builder: Programmatically create FSM transitions
  • Workflow Extensions: Structured app-based workflow modifications
  • Migration-Free: All extensions work without requiring database migrations

Installation

pip install django-fsm-dynamic

Requirements:

  • Python 3.10+
  • Django 5.0+
  • django-fsm-2 4.0+

Quick Start

1. Create a Dynamic State Enum

from django_fsm_dynamic import DynamicStateEnum
from django_fsm import FSMIntegerField
from django.db import models

class BlogPostStateEnum(DynamicStateEnum):
    NEW = 10
    PUBLISHED = 20
    HIDDEN = 30

class BlogPost(models.Model):
    title = models.CharField(max_length=200)
    state = FSMIntegerField(
        default=BlogPostStateEnum.NEW,
        choices=BlogPostStateEnum.get_choices  # Prevents migrations!
    )

2. Extend from Another App

# In your review app's apps.py
from django.apps import AppConfig
from django_fsm_dynamic import WorkflowExtension, TransitionBuilder

class ReviewWorkflowExtension(WorkflowExtension):
    target_model = 'blog.BlogPost'
    target_enum = 'blog.models.BlogPostStateEnum'
    
    def extend_states(self, enum_class):
        enum_class.add_state('IN_REVIEW', 15)
        enum_class.add_state('APPROVED', 17)
    
    def extend_transitions(self, model_class, enum_class):
        builder = TransitionBuilder(model_class)
        builder.add_transition('send_to_review', enum_class.NEW, enum_class.IN_REVIEW)
        builder.add_transition('approve', enum_class.IN_REVIEW, enum_class.APPROVED)
        builder.build_and_attach()

class ReviewConfig(AppConfig):
    name = 'review'
    
    def ready(self):
        ReviewWorkflowExtension(self).apply()

3. Use the Extended Workflow

# Create a blog post
post = BlogPost.objects.create(title="My Post", state=BlogPostStateEnum.NEW)

# Use dynamically added transitions
post.send_to_review()  # NEW -> IN_REVIEW
post.approve()         # IN_REVIEW -> APPROVED

Core Components

DynamicStateEnum

Base class for extensible state enums:

from django_fsm_dynamic import DynamicStateEnum

class MyStateEnum(DynamicStateEnum):
    NEW = 10
    PUBLISHED = 20

# Other apps can extend:
MyStateEnum.add_state('IN_REVIEW', 15)

# Get all choices including dynamic ones:
choices = MyStateEnum.get_choices()  # [(10, 'New'), (15, 'In Review'), (20, 'Published')]

Dynamic Choices

Use the get_choices method directly to prevent Django migrations:

class MyModel(models.Model):
    state = FSMIntegerField(
        default=MyStateEnum.NEW,
        choices=MyStateEnum.get_choices  # No migrations when enum changes!
    )

TransitionBuilder

Programmatically create FSM transitions:

from django_fsm_dynamic import TransitionBuilder

builder = TransitionBuilder(MyModel)
builder.add_transition(
    'approve', 
    source=MyStateEnum.IN_REVIEW,
    target=MyStateEnum.APPROVED,
    conditions=[lambda instance: instance.is_valid()],
    permission='myapp.can_approve'
).build_and_attach()

WorkflowExtension

Structured approach to extending workflows:

from django_fsm_dynamic import WorkflowExtension

class MyExtension(WorkflowExtension):
    target_model = 'app.Model'
    target_enum = 'app.models.StateEnum'
    
    def extend_states(self, enum_class):
        enum_class.add_state('NEW_STATE', 99)
    
    def extend_transitions(self, model_class, enum_class):
        # Add new transitions
        pass
    
    def modify_existing_transitions(self, model_class, enum_class):
        # Modify existing transitions
        pass

Testing

Quick Testing (Single Environment)

  • Run all tests: python -m django test --settings=tests.settings (from project root)
  • Run specific test: python -m django test tests.test_dynamic_utilities --settings=tests.settings
  • Run with coverage: coverage run -m django test --settings=tests.settings && coverage report

Matrix Testing (Multiple Python/Django Versions)

This project supports testing across multiple Python (3.10-3.14) and Django (5.0-5.2) versions using nox:

  • Install testing dependencies: uv pip install -e .[dev]
  • Run all stable test combinations: nox
  • Run tests for specific Python version: nox -s tests-3.12
  • Run tests for specific Django version: nox -s tests-3.12 -- --django=5.2
  • Run experimental tests (Python 3.14, Django main): nox -s tests_experimental_python tests_experimental_django
  • Run linting: nox -s lint
  • Run formatting check: nox -s format
  • Run coverage report: nox -s coverage

Available Python Versions

The project is tested against:

  • Stable: Python 3.10, 3.11, 3.12, 3.13
  • Experimental: Python 3.14 (allowed to fail)

Available Django Versions

The project is tested against:

  • Stable: Django 5.0, 5.1, 5.2
  • Experimental: Django main branch (allowed to fail)

Documentation

Why Separate Package?

Dynamic workflows are a powerful but specialized feature. By extracting them into a separate package:

  1. Focused Development: Each package has a clear, focused scope
  2. Optional Dependency: Only install if you need dynamic workflows
  3. Independent Versioning: Features can evolve independently
  4. Cleaner Core: django-fsm-2 stays focused on core FSM functionality

Contributing

We welcome contributions! Please see our Contributing Guide for details.

License

MIT License. See LICENSE for details.

Changelog

See CHANGELOG.md for version history.

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_fsm_dynamic-1.1.0.tar.gz (18.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_fsm_dynamic-1.1.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file django_fsm_dynamic-1.1.0.tar.gz.

File metadata

  • Download URL: django_fsm_dynamic-1.1.0.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for django_fsm_dynamic-1.1.0.tar.gz
Algorithm Hash digest
SHA256 13505120bcb784b1ff1365458f59e71a99c600f77ceda4a2053cc72d7ae9c1b0
MD5 0528f9816c5a3d20875b6dac2abb775d
BLAKE2b-256 bb41bfdc7bfd7b32ee23f769d3a467a51e7e5963027a4d4cce639f1f11404006

See more details on using hashes here.

File details

Details for the file django_fsm_dynamic-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_fsm_dynamic-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 038fe3f18ed7f69522319df88b97d6908a033c0324460c7827b2c23e45862fef
MD5 2d172e6cee21765c6bd0e44e6526b188
BLAKE2b-256 9baf42c037887b7b7ba5951bcc0cfae2d2cb51ce65581284122a2ca51be2a9a8

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