Skip to main content

django-pgconstraints

PyPI version Python versions CI

Declarative PostgreSQL constraint triggers for Django, with foreign-key traversal that the built-in constraints can't express.

UniqueConstraint, CheckConstraint, and GeneratedField in Django 6.0 compile down to plain PostgreSQL constraints — so they can only reference columns on the same table. django-pgconstraints provides three trigger-based classes that accept F("related__field") expressions and compile to PL/pgSQL triggers via django-pgtrigger. Their API mirrors the Django equivalents to keep the mental model familiar.

from django.db.models import F, Q
from django_pgconstraints import CheckConstraintTrigger

class OrderLine(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    quantity = models.IntegerField()

    class Meta:
        triggers = [
            CheckConstraintTrigger(
                condition=Q(quantity__lte=F("product__stock")),
                name="orderline_qty_lte_stock",
            ),
        ]

What's in the box

  • UniqueConstraintTrigger — unique constraint whose fields= accepts FK chains ("book__author"). Also supports Django expressions, partial conditions, nulls_distinct, and deferred timing.
  • CheckConstraintTrigger — check constraint whose Q can cross foreign keys (Q(quantity__lte=F("product__stock"))).
  • GeneratedFieldTrigger — a computed column whose expression can reference related rows. The package automatically installs reverse triggers so the value stays in sync when that related data changes, and piggybacks RETURNING on save() / bulk_create() so the computed value lands on the Python instance with no extra query.

Each trigger also integrates with Django's Model.full_clean() so ORM-level validation still catches violations before they hit the database, and each accepts violation_error_code / violation_error_message so the errors raised by full_clean() match your application's error-handling conventions.

Install

pip install django-pgconstraints
# or
uv add django-pgconstraints

Add pgtrigger and django_pgconstraints to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    "pgtrigger",
    "django_pgconstraints",
    ...
]

Triggers go in Meta.triggers (a django-pgtrigger extension), not Meta.constraints. The package ships a system check (pgconstraints.E001) that flags the common mistake.

Requirements

  • Python 3.14+
  • Django 6.0
  • PostgreSQL (any currently supported version)
  • django-pgtrigger 4.17+

Documentation

Full documentation at oliverhaas.github.io/django-pgconstraints.

Runnable examples live in examples/:

License

MIT

Metadata

Release files for django-pgconstraints 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-pgconstraints 0.2.0
File Size Uploaded
django_pgconstraints-0.2.0.tar.gz 23.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-pgconstraints 0.2.0
File Interpreter ABI Platform
django_pgconstraints-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 50.2 kB

Release files / django_pgconstraints-0.2.0.tar.gz

Download URL django_pgconstraints-0.2.0.tar.gz
Size 23.2 kB
Tags Source
SHA-256 checksum
How to use checksums
246d181777ae9c1b2e96f4ab4e690808bd29d4d34c9fef0ac0661146d9b61cdd
BLAKE2b-256 checksum
How to use checksums
9e91bbb0930e3b325539089293bd5a89d198d0e00123a75f021816dd055593ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 23, 2026.

Transparency log

Release files / django_pgconstraints-0.2.0-py3-none-any.whl

Download URL django_pgconstraints-0.2.0-py3-none-any.whl
Size 27.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f57b31ebd2a70c6a43644d05ea56664297c1a107271534d2caec6c6b59ac85a
BLAKE2b-256 checksum
How to use checksums
cbc47553e802ab21d5b87c89d3117a70e7d1f21935c03fdc9fa3734431601d5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 23, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page