---
title: "django-pgtrigger | pypx"
description: "Postgres trigger support integrated with Django models."
canonical_url: "https://pypx.app/packages/django-pgtrigger"
meta:
  "og:description": "Postgres trigger support integrated with Django models."
  "og:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_django-pgtrigger%2Cversion_4.17.0%2Csummary_Postgres%2Btrigger%2Bsupport%2Bintegrated%2Bwith%2BDjango%2Bmodels.%2Clicense_BSD%2BLicense%2Cp_Ii9wYWNrYWdlcy9kamFuZ28tcGd0cmlnZ2VyIg%2Cs_ypjmvF5-pBjmvzTl.png"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "django-pgtrigger 4.17.0"
  "twitter:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_django-pgtrigger%2Cversion_4.17.0%2Csummary_Postgres%2Btrigger%2Bsupport%2Bintegrated%2Bwith%2BDjango%2Bmodels.%2Clicense_BSD%2BLicense%2Cp_Ii9wYWNrYWdlcy9kamFuZ28tcGd0cmlnZ2VyIg%2Cs_ypjmvF5-pBjmvzTl.png"
---

> Index of every page on this site: [llms.txt](https://pypx.app/llms.txt)

# **django-pgtrigger**

v4.17.0

Postgres trigger support integrated with Django models.

35 KB Python 3.10.0+ py3-none-anyBSD License1 deps

[** Diff **](https://pypx.app/packages/django-pgtrigger/diff)

`$ uv add django-pgtrigger`

# django-pgtrigger

`django-pgtrigger` helps you write [Postgres triggers](https://www.postgresql.org/docs/current/sql-createtrigger.html) for your Django models.

## Why should I use triggers?

Triggers can solve a variety of complex problems more reliably, performantly, and succinctly than application code. For example,

- Protecting operations on rows or columns ( `pgtrigger.Protect`).
- Making read-only models or fields ( `pgtrigger.ReadOnly`).
- Soft-deleting models ( `pgtrigger.SoftDelete`).
- Snapshotting and tracking model changes ([django-pghistory](https://django-pghistory.readthedocs.io/)).
- Enforcing field transitions ( `pgtrigger.FSM`).
- Keeping a search vector updated for full-text search ( `pgtrigger.UpdateSearchVector`).
- Building official interfaces (e.g. enforcing use of `User.objects.create_user` and not `User.objects.create`).
- Versioning models, mirroring fields, computing unique model hashes, and the list goes on...

All of these examples require no overridden methods, no base models, and no signal handling.

## Quick start

Install `django-pgtrigger` with `pip3 install django-pgtrigger` and add `pgtrigger` to `settings.INSTALLED_APPS`.

`pgtrigger.Trigger` objects are added to `triggers` in model `Meta`. `django-pgtrigger` comes with several trigger classes, such as `pgtrigger.Protect`. In the following, we're protecting the model from being deleted:

```
import pgtrigger

class ProtectedModel(models.Model):
    """This model cannot be deleted!"""

    class Meta:
        triggers = [
            pgtrigger.Protect(name="protect_deletes", operation=pgtrigger.Delete)
        ]
```

When migrations are created and executed, `ProtectedModel` will raise an exception anytime a deletion is attempted.

Let's extend this example further and only protect deletions on inactive objects. In this example, the trigger conditionally runs when the row being deleted (the `OLD` row in trigger terminology) is still active:

```
import pgtrigger

class ProtectedModel(models.Model):
    """Active object cannot be deleted!"""
    is_active = models.BooleanField(default=True)

    class Meta:
        triggers = [
            pgtrigger.Protect(
                name="protect_deletes",
                operation=pgtrigger.Delete,
                condition=pgtrigger.Q(old__is_active=True)
            )
        ]
```

`django-pgtrigger` uses `pgtrigger.Q` and `pgtrigger.F` objects to conditionally execute triggers based on the `OLD` and `NEW` rows. Combining these Django idioms with `pgtrigger.Trigger` objects can solve a wide variety of problems without ever writing SQL. Users, however, can still use raw SQL for complex cases.

Triggers are installed like other database objects. Run `python manage.py makemigrations` and `python manage.py migrate` to install triggers.

If triggers are new to you, don't worry. The [pgtrigger docs](https://django-pgtrigger.readthedocs.io/) cover triggers in more detail and provide many examples.

## Compatibility

`django-pgtrigger` is compatible with Python 3.10 - 3.14, Django 4.2 - 6.0, Psycopg 2 - 3, and Postgres 14 - 18.

## Documentation

[View the django-pgtrigger docs here](https://django-pgtrigger.readthedocs.io/) to learn more about:

- Trigger basics and motivation for using triggers.
- How to use the built-in triggers and how to build custom ones.
- Installing triggers on third-party models, many-to-many fields, and other advanced scenarios.
- Writing conditional triggers.
- Ignoring triggers dynamically and deferring trigger execution.
- Multiple database, schema, and partitioning support.
- Frequently asked questions, common issues, and upgrading.
- The commands, settings, and module.

## Installation

Install `django-pgtrigger` with:

```
pip3 install django-pgtrigger
```

After this, add `pgtrigger` to the `INSTALLED_APPS` setting of your Django project.

## Other Material

After you've read the docs, check out [this tutorial](https://wesleykendall.github.io/django-pgtrigger-tutorial/) with interactive examples from a Django meetup talk.

The [DjangoCon 2021 talk](https://www.youtube.com/watch?v=Tte3d4JjxCk) also breaks down triggers and shows several examples.

## Contributing Guide

For information on setting up django-pgtrigger for development and contributing changes, view [CONTRIBUTING.md](https://pypx.app/CONTRIBUTING.md).

## Creators

- [Wes Kendall](https://github.com/wesleykendall)

## Other Contributors

- @jzmiller1
- @rrauenza
- @ralokt
- @adamchainz
- @danifus
- @kekekekule
- @peterthomassen
- @pfouque

## **Details **

<dl>

<dt>Version</dt>
<dd>4.17.0</dd>

<dt>License</dt>
<dd>BSD License</dd>

<dt>Python</dt>
<dd>&lt;4,&gt;=3.10.0</dd>

<dt>Maintainer</dt>
<dd>Wes Kendall</dd></dl>

## **Links**

- [Documentation](https://django-pgtrigger.readthedocs.io)
- [Homepage](https://github.com/AmbitionEng/django-pgtrigger)
- [Repository](https://github.com/AmbitionEng/django-pgtrigger)
- [View on PyPI](https://pypi.org/project/django-pgtrigger/)

## **Release Cadence **

**2**

releases in the past year

avg 35 days between releases

## **Maintainers **

- Wes Kendall

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://pypx.app/#website","@type":"WebSite","description":"The Python Package Index, reimagined. Fast search, dependency insights, download trends, and changelogs — all in one place.","name":"pypx","url":"https://pypx.app/"},{"@id":"https://pypx.app/packages/django-pgtrigger#webpage","@type":"WebPage","description":"Postgres trigger support integrated with Django models.","name":"django-pgtrigger","url":"https://pypx.app/packages/django-pgtrigger","isPartOf":{"@id":"https://pypx.app/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://pypx.app/packages/django-pgtrigger"]}]},{"@id":"https://pypx.app/packages/django-pgtrigger#/schema/software-application/1","@type":"SoftwareApplication","applicationCategory":"DeveloperApplication","description":"Postgres trigger support integrated with Django models.","license":"BSD License","name":"django-pgtrigger","softwareVersion":"4.17.0"}]}
```

## Sitemap

See the full [sitemap](https://pypx.app/sitemap.md) for all pages.
