Django Integration

Django-specific components for airlock.

from airlock.integrations.django import DjangoScope, AirlockMiddleware

Configuration

Configure via settings.py:

AIRLOCK = {
    "POLICY": "airlock.AllowAll",  # Dotted path or callable
    "EXECUTOR": "airlock.integrations.executors.celery.celery_executor",
    "SCOPE": "airlock.integrations.django.DjangoScope",
}

Classes

DjangoScope

Bases: Scope

A Django-specific scope that respects database transactions.

Defers dispatch to transaction.on_commit() so side effects only fire after the transaction commits successfully. When called outside a transaction (autocommit mode), on_commit executes immediately.

If no executor is provided, uses get_executor() to select one based on EXECUTOR setting.

Subclass and override schedule_dispatch() to customize dispatch timing.

schedule_dispatch

schedule_dispatch(callback: Callable[[], None]) -> None

Schedule the dispatch callback. Override to customize dispatch timing.

By default, uses transaction.on_commit(). On Django 5.0+, uses robust=True so one failing callback doesn't prevent others from running. This defers dispatch until the transaction commits, or runs immediately if outside a transaction (autocommit mode).

Override to change timing, robust behavior, or skip on_commit entirely.

AirlockMiddleware

Django middleware that wraps each request in an airlock scope.

By default:

  • 1xx/2xx/3xx responses: flush
  • 4xx/5xx responses or exceptions: discard

Subclass and override should_flush() for custom behavior.

should_flush

should_flush(request, response) -> bool

Override to customize flush behavior.

Returns:
  • bool

    True to flush (dispatch intents), False to discard.

Functions

get_executor

get_executor() -> Executor

Get the appropriate executor based on EXECUTOR setting.

EXECUTOR should be a dotted import path to an executor callable, or None for synchronous execution.

Example::

AIRLOCK = {
    'EXECUTOR': 'airlock.integrations.executors.django_q.django_q_executor',
}

# Or use a custom executor
AIRLOCK = {
    'EXECUTOR': 'myapp.executors.custom_executor',
}
Returns:
  • Executor

    Executor function based on EXECUTOR setting.

Raises:
  • ImportError

    If the executor module/callable cannot be imported.

get_policy

get_policy()

Get the policy instance based on POLICY setting.

get_scope_class

get_scope_class()

Get the scope class to use, based on SCOPE setting.