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
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)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_pgconstraints-0.2.0.tar.gz | 23.2 kB | With provenance | Details for django_pgconstraints-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_pgconstraints-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for django_pgconstraints-0.2.0-py3-none-any.whl |
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 logRelease 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 logSecurity for django-pgconstraints 0.2.0
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for django_pgconstraints-0.2.0.tar.gz
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 logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.