Declarative PostgreSQL constraint triggers for Django
Project description
django-pgconstraints
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 whosefields=accepts FK chains ("book__author"). Also supports Django expressions, partial conditions,nulls_distinct, and deferred timing.CheckConstraintTrigger— check constraint whoseQcan 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 piggybacksRETURNINGonsave()/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/:
examples/simple/— cross-publisher unique chapter namesexamples/full/— inventory + purchase orders exercising all three triggers
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_pgconstraints-0.2.0.tar.gz.
File metadata
- Download URL: django_pgconstraints-0.2.0.tar.gz
- Upload date:
- Size: 23.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
246d181777ae9c1b2e96f4ab4e690808bd29d4d34c9fef0ac0661146d9b61cdd
|
|
| MD5 |
7c9585fb35177b37bd52d766275db80a
|
|
| BLAKE2b-256 |
9e91bbb0930e3b325539089293bd5a89d198d0e00123a75f021816dd055593ac
|
Provenance
The following attestation bundles were made for django_pgconstraints-0.2.0.tar.gz:
Publisher:
publish.yml on oliverhaas/django-pgconstraints
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_pgconstraints-0.2.0.tar.gz -
Subject digest:
246d181777ae9c1b2e96f4ab4e690808bd29d4d34c9fef0ac0661146d9b61cdd - Sigstore transparency entry: 1364792304
- Sigstore integration time:
-
Permalink:
oliverhaas/django-pgconstraints@e0e464c85fbb1d983e0ecd9e02ae8da5da0af8a4 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/oliverhaas
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e0e464c85fbb1d983e0ecd9e02ae8da5da0af8a4 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file django_pgconstraints-0.2.0-py3-none-any.whl.
File metadata
- Download URL: django_pgconstraints-0.2.0-py3-none-any.whl
- Upload date:
- Size: 27.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4f57b31ebd2a70c6a43644d05ea56664297c1a107271534d2caec6c6b59ac85a
|
|
| MD5 |
91388dc063f48b66e7bb02c9b9bab5a9
|
|
| BLAKE2b-256 |
cbc47553e802ab21d5b87c89d3117a70e7d1f21935c03fdc9fa3734431601d5b
|
Provenance
The following attestation bundles were made for django_pgconstraints-0.2.0-py3-none-any.whl:
Publisher:
publish.yml on oliverhaas/django-pgconstraints
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_pgconstraints-0.2.0-py3-none-any.whl -
Subject digest:
4f57b31ebd2a70c6a43644d05ea56664297c1a107271534d2caec6c6b59ac85a - Sigstore transparency entry: 1364792352
- Sigstore integration time:
-
Permalink:
oliverhaas/django-pgconstraints@e0e464c85fbb1d983e0ecd9e02ae8da5da0af8a4 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/oliverhaas
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@e0e464c85fbb1d983e0ecd9e02ae8da5da0af8a4 -
Trigger Event:
workflow_dispatch
-
Statement type: