Registry / database / django-pgtrigger

django-pgtrigger

JSON →
library4.17.0pypypi✓ verified 23d ago

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-pgtrigger
INSTALL
IMPORT
SIG · DJANGO-PGTRIGGER
D
django-pgtrigger
databasepythonv4.17.0
Install
3.5s avg
Import
Disk
66MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.17.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 66.6MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.5s · import 0.000s · 67MB
66MB installed
● package 66MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pgtrigger
import pgtrigger
import pgtrigger

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.

import pgtrigger from django.db import models class ProtectedModel(models.Model): name = models.CharField(max_length=100) class Meta: app_label = 'myapp' triggers = [ pgtrigger.Protect(name="prevent_deletes", operation=pgtrigger.Delete) ] # To see this in action, after adding 'pgtrigger' to INSTALLED_APPS and 'myapp' to your Django project: # 1. python manage.py makemigrations # 2. python manage.py migrate # 3. From a Django shell: # from myapp.models import ProtectedModel # ProtectedModel.objects.create(name='Test') # ProtectedModel.objects.first().delete() # This will raise a ProgrammingError from PostgreSQL
Debug
Known issues
breakingVersion 4.16.0 dropped support for Python 3.9, and version 4.14.0 dropped support for Postgres 13. Users on these older environments must upgrade their Python or PostgreSQL versions, or pin to an older django-pgtrigger version.
fix
Upgrade Python to 3.10+ and PostgreSQL to 14+ or pin `django-pgtrigger<4.14.0` or `django-pgtrigger<4.16.0` respectively.
affects: 4.14.0, 4.16.0
breakingVersion 4.0.0 introduced significant changes to multi-database and registry behavior. The `pgtrigger.get` function was renamed to `pgtrigger.registered`. Management commands (`install`, `uninstall`, `prune`, etc.) now operate on one database at a time and require the `--database` argument for multi-database setups.
fix
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.
affects: 4.0.0+
deprecatedThe `@pgtrigger.register` decorator on models is deprecated since version 3.0. While still functional, the recommended and fully supported way to define triggers is within the `Meta.triggers` list of your Django model.
fix
Migrate from using `@pgtrigger.register(...)` as a decorator to defining a `triggers` list in your model's `Meta` class, for example, `class Meta: triggers = [...]`.
affects: 3.0+
gotchaIf `settings.PGTRIGGER_MIGRATIONS` is set to `False` to disable migration integration, triggers will not be automatically installed or updated via Django migrations. Manual installation via `python manage.py pgtrigger install` or setting `PGTRIGGER_INSTALL_ON_MIGRATE = True` is then required.
fix
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.
affects: All versions
gotchaRegistering triggers programmatically on models from third-party Django applications can create migrations in those apps, leading to potential issues with migration management. Additionally, altering column types used in trigger conditions can cause migration failures if `settings.PGTRIGGER_SCHEMA_EDITOR` is `False`.
fix
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.
affects: All versions
Upgrade
Version history
4.17.0latest on PyPI · released Dec 4, 2025
Audit
Dependencies
DjangorequiredCore framework integration; compatible with Django 4.2 - 6.0.
psycopg2/psycopgoptionalPostgreSQL adapter for Python; compatible with Psycopg 2 - 3.
PostgreSQLoptionalDatabase server where triggers are installed; compatible with Postgres 14 - 18.
Agent activity
19 hits · last 30 days
node
18
Resources
django-pgtrigger — pip install django-pgtrigger · libregistry