Skip to main content

Postgres trigger support integrated with Django models.

Project description

django-pgtrigger

django-pgtrigger helps you write Postgres triggers 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).
  • 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 cover triggers in more detail and provide many examples.

Compatibility

django-pgtrigger is compatible with Python 3.9 - 3.13, Django 4.2 - 5.1, Psycopg 2 - 3, and Postgres 13 - 17.

Documentation

View the django-pgtrigger docs here 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 with interactive examples from a Django meetup talk.

The DjangoCon 2021 talk 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.

Creators

Other Contributors

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

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_pgtrigger-4.13.2.tar.gz (30.7 kB view details)

Uploaded Source

Built Distribution

django_pgtrigger-4.13.2-py3-none-any.whl (34.1 kB view details)

Uploaded Python 3

File details

Details for the file django_pgtrigger-4.13.2.tar.gz.

File metadata

  • Download URL: django_pgtrigger-4.13.2.tar.gz
  • Upload date:
  • Size: 30.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/5.15.0-1057-aws

File hashes

Hashes for django_pgtrigger-4.13.2.tar.gz
Algorithm Hash digest
SHA256 f4d906df3a6dde1abc42b88b351bd942c180fe28dfb664ff4e766ebe2bd8fd81
MD5 86618b2e2c579307cae6352386bafe1a
BLAKE2b-256 a538ad22acd0f2651f5bf61f66a92026136124130bf0b4f6881fcefc628653b7

See more details on using hashes here.

File details

Details for the file django_pgtrigger-4.13.2-py3-none-any.whl.

File metadata

  • Download URL: django_pgtrigger-4.13.2-py3-none-any.whl
  • Upload date:
  • Size: 34.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/5.15.0-1057-aws

File hashes

Hashes for django_pgtrigger-4.13.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0f6abd76db30c05c16108f474011650448bd6129cf68e376ec7d40d72fea1c9a
MD5 a072daa0037ca98cfca5c46305ddc88e
BLAKE2b-256 da7195110a13beb4312530e151e9b4c5a2eda3d022059779922b8cce7bdec50b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page