django-pgtrigger is a Python library that enables the creation and management of PostgreSQL triggers directly from Django models. It simplifies the process of integrating powerful database-level logic, such as protecting operations, enforcing read-only fields, or soft-deleting models, into Django applications. The library is actively maintained, frequently releasing new versions to support the latest Django, Python, and PostgreSQL releases.
pip install django-pgtriggerVerified import paths — ran on the pinned version, not inferred.
To quickly get started, install `django-pgtrigger` and add `pgtrigger` to your `INSTALLED_APPS`. Define triggers directly in your Django model's `Meta.triggers` list using built-in trigger classes like `pgtrigger.Protect`. After defining, run `makemigrations` and `migrate` to install the triggers in your PostgreSQL database.
Upgrade Python to 3.10+ and PostgreSQL to 14+ or pin `django-pgtrigger<4.14.0` or `django-pgtrigger<4.16.0` respectively.
Update calls from `pgtrigger.get` to `pgtrigger.registered`. For multi-database environments, ensure management commands include the `--database` argument. Run `python manage.py pgtrigger install` or `prune` to update old triggers.
Migrate from using `@pgtrigger.register(...)` as a decorator to defining a `triggers` list in your model's `Meta` class, for example, `class Meta: triggers = [...]`.
Either enable `PGTRIGGER_MIGRATIONS` (default `True`), or ensure a post-deploy step runs `python manage.py pgtrigger install`, or set `PGTRIGGER_INSTALL_ON_MIGRATE = True` in settings.
For third-party models, create a proxy model and define triggers in its `Meta.triggers`. Ensure `PGTRIGGER_SCHEMA_EDITOR` remains `True` (default) if you anticipate changing column types involved in trigger conditions.