• Stars
    star
    524
  • Rank 84,541 (Top 2 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created over 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Write Postgres triggers for your Django models

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:

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:

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.7 - 3.11, Django 3.2 - 4.2, Psycopg 2 - 3, and Postgres 10 - 15.

Documentation

View the 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.
  • 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.rst.

Primary Authors

Other Contributors

  • @jzmiller1
  • @rrauenza
  • @ralokt
  • @adamchainz

More Repositories

1

django-pghistory

Track historical events to Django models using Postgres triggers.
Python
322
star
2

django-pgtransaction

A context manager/decorator which extends Django's atomic function with the ability to set isolation level and retries for a given transaction.
Python
51
star
3

django-pgbulk

Native postgres bulk update and upsert operations.
Python
50
star
4

django-pgclone

Dump and restore Postgres databases with Django.
Python
34
star
5

git-tidy

Tidy git commit messages, linting, and logging
Python
28
star
6

django-pglock

Postgres advisory locks, table locks, and blocking lock management.
Python
26
star
7

django-pgstats

Commands and models for tracking internal postgres stats.
Python
19
star
8

django-pgconnection

Route postgres connections and hook into cursor execution
Python
17
star
9

django-migration-docs

Sync and validate additional information about your Django migrations.
Python
12
star
10

django-pgmigrate

Avoid costly downtime during Postgres migrations.
Python
11
star
11

qik

Run cached commands in your Python monorepo.
Python
11
star
12

django-strict-fields

A collection of fields and utilities to help make model fields more strict.
Python
10
star
13

django-pgactivity

View, filter, and kill Postgres queries.
Python
10
star
14

formaldict

Formal structured dictionaries parsed from a schema
Python
8
star
15

footing

Keep templated projects in sync with their template
Python
7
star
16

python-library-template

The template for all public Django apps by Opus 10
Python
4
star
17

detail

Build automations off of structured notes in your project
Python
3
star
18

public-python-library-template

The template for all public python packages by Opus 10
Python
2
star
19

django-pgpartition

Partition Django models.
Python
1
star
20

django-pgstrictjson

JSON fields with trigger-based validation.
Python
1
star