Skip to main content

Postgres trigger support integrated with Django models.

Project description

django-pgtrigger provides primitives for configuring Postgres triggers on Django models.

Triggers can solve a wide variety of database-level problems more elegantly and reliably than in the application-level of Django. Here are some common problems that can be solved with triggers, many of which we later show how to solve in the docs:

  1. Protecting updates and deletes or rows or columns (pgtrigger.Protect).

  2. Soft deleting models by setting a field to a value on delete (pgtrigger.SoftDelete).

  3. Tracking changes to models or columns change, or when specific conditions happen (django-pghistory uses django-pgtrigger to do this).

  4. Keeping fields in sync with other fields.

  5. Ensuring that engineers use an official interface (e.g. engineers must use User.objects.create_user and not User.objects.create).

  6. Only allowing a status field of a model to transition through certain states (pgtrigger.FSM).

Quick Start

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

Models are decorated with @pgtrigger.register and supplied with pgtrigger.Trigger objects. If you don’t have access to the model definition, you can still call pgtrigger.register programmatically.

Users declare the plpgsql code manually in a pgtrigger.Trigger object or can use the derived triggers in django-pgtrigger that implement common scenarios. For example, pgtrigger.Protect can protect operations on a model, such as deletions:

from django.db import models
import pgtrigger


@pgtrigger.register(pgtrigger.Protect(operation=pgtrigger.Delete))
class CannotBeDeletedModel(models.Model):
    """This model cannot be deleted!"""

django-pgtrigger aims to alleviate the boilerplate of triggers and having to write raw SQL by using common Django idioms. For example, users can use pgtrigger.Q and pgtrigger.F objects to conditionally execute triggers based on the OLD and NEW row being modified. For example, let’s only protect deletes against “active” rows of a model:

from django.db import models
import pgtrigger


@pgtrigger.register(
    pgtrigger.Protect(
        operation=pgtrigger.Delete,
        # Protect deletes when the OLD row of the trigger is still active
        condition=pgtrigger.Q(old__is_active=True)
    )
)
class CannotBeDeletedModel(models.Model):
    """Active model object cannot be deleted!"""
    is_active = models.BooleanField(default=True)

Combining pgtrigger.Q, pgtrigger.F, and derived pgtrigger.Trigger objects can solve a wide array of Django problems without ever having to write raw SQL. Users, however, can still customize triggers and write as much raw SQL as needed for their use case.

Tutorial

For a complete run-through of django-pgtrigger and all derived triggers (along with a trigger cookbook!), read the pgtrigger docs. The docs have a full tutorial of how to configure triggers and lots of code examples.

After you have gone through the tutorial in the docs, check out https://wesleykendall.github.io/django-pgtrigger-tutorial/, which is an interactive tutorial written for a Django meetup talk about django-pgtrigger.

Documentation

View the django-pgtrigger docs here.

Installation

Install django-pgtrigger with:

pip3 install django-pgtrigger

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

Contributing Guide

For information on setting up django-pgtrigger for development and contributing changes, view CONTRIBUTING.rst.

Primary Authors

  • @wesleykendall (Wes Kendall)

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-2.3.0.tar.gz (24.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_pgtrigger-2.3.0-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

Details for the file django-pgtrigger-2.3.0.tar.gz.

File metadata

  • Download URL: django-pgtrigger-2.3.0.tar.gz
  • Upload date:
  • Size: 24.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.8.2 Linux/4.15.0-1092-aws

File hashes

Hashes for django-pgtrigger-2.3.0.tar.gz
Algorithm Hash digest
SHA256 cf2f55e955161c69b9be5657796a751085a71250200e3d21db938f76cc2da057
MD5 207bf2723e058bf4ecee50a37aef33b4
BLAKE2b-256 9694b29afb134872d820ed7c4e3f02c57bcd7257476d2c7d6197cd18ad02a6b8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_pgtrigger-2.3.0-py3-none-any.whl
  • Upload date:
  • Size: 27.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.8.2 Linux/4.15.0-1092-aws

File hashes

Hashes for django_pgtrigger-2.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4ab1fafaa6cc196153edd8c14af7f408cb504d15d885281f46482c3e5031a10a
MD5 b4da41c2712b6eb5794bec0ff2ea919d
BLAKE2b-256 c666b9401edaa303c58a5d867714371a46901131ca58503373dff26f05aeca45

See more details on using hashes here.

Supported by

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