django-postgres-denormalize
PostgreSQL cannot index, sort or constrain on a value that is stored in another table. A value that is several joins away, such as the tenant that a row belongs to through its parent, cannot be part of an index or a unique constraint on the row’s table, and every query that filters or orders by it pays for the joins.
This package lets a model store a copy of such a value in an ordinary column, and keeps the copy correct with PostgreSQL triggers. That works for every write, whether it comes from the ORM, a bulk update, raw SQL or the previous release that is still serving during a rolling deploy. The model declares the lookup path and nothing else; makemigrations writes the column, the triggers and their changes.
Full documentation is at https://django-postgres-denormalize.readthedocs.io/.
from django.db import models
from postgres_denormalize.fields import DenormalizedField, DenormalizedForeignKey
class Task(models.Model):
project = models.ForeignKey('Project', on_delete=models.CASCADE)
title = models.CharField(max_length=100)
# A copy of project.tenant: a real foreign key, its target and on_delete rule inferred from the path.
tenant = DenormalizedForeignKey('project__tenant')
# A copy of project.name: a column of the same type as the source.
project_name = DenormalizedField('project__name')
class Meta:
ordering = ['project_name', 'title']
constraints = [models.UniqueConstraint(fields=['tenant', 'title'], name='unique_task_title_per_tenant')]
indexes = [models.Index(fields=['tenant', 'project_name', 'title'], name='task_listing')]
From then on, saving a task computes its copies in the database and reads them back onto the instance. Moving a project to another tenant, or renaming it, rewrites the copies of its tasks in the same transaction, but only the copies that change.
Use it for values that an index or a unique constraint needs, and values that most queries sort or filter by across several joins. Do not use it to save a join for rendering, because select_related does that for free. Do not use it either for values whose source is often updated in bulk by code that you cannot wrap in one of its batch helpers. See when to use it, and when not to, and the alternatives page, which compares it with django-pgconstraints and django-computedfields.
Installation
pip install django-postgres-denormalize
INSTALLED_APPS = [
# ...
'pgtrigger',
'postgres_denormalize',
]
Requires PostgreSQL and django-pgtrigger, with pgtrigger’s migration support turned on (the default). See the settings reference for the options.
Relationship to DjanQuiltDB
While this library was written in conjunction with DjanQuiltDB and the libraries were designed to work together seamlessly in projects where both are installed, both libraries are written to be used as standalone libraries as well. For using django-postgres-denormalize, it is not necessary to use or be familiar with DjanQuiltDB.
If you wish to use DjanQuiltDB features for the copies that django-postgres-denormalize keeps, you can install the djanquiltdb[postgres-denormalize] extra. For more information, please refer to the DjanQuiltDB documentation.
Metadata
Release files for django-postgres-denormalize 1.0.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 | |
|---|---|---|---|---|
| django_postgres_denormalize-1.0.0.tar.gz | 171.1 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_postgres_denormalize-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 211.8 kB
Release files / django_postgres_denormalize-1.0.0.tar.gz
| Download URL | django_postgres_denormalize-1.0.0.tar.gz |
|---|---|
| Size | 171.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3e2a1b33fb8f466a2bba6c704a9f1b2250a361ab7926fac7f538159bf5b9c397
|
|
BLAKE2b-256 checksum How to use checksums |
44fbcbc56035fb249d3ab574cec7648a3171b96b6f4959393f9dc525d96bfbad
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 18, 2026.
Transparency logRelease files / django_postgres_denormalize-1.0.0-py3-none-any.whl
| Download URL | django_postgres_denormalize-1.0.0-py3-none-any.whl |
|---|---|
| Size | 40.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
049f52ebd2431e3c663e7205c4408be502153d4ad41441d4e4a4ce215e44a6fe
|
|
BLAKE2b-256 checksum How to use checksums |
d0825dd2d7ae5a757f4ffa927ebef79573b0a32e6581cbfbb6c2fae51e456540
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Oct 18, 2026.
Transparency logSecurity for django-postgres-denormalize 1.0.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
All 2 files in this release have been uploaded with provenance metadata.
Attestation for django_postgres_denormalize-1.0.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 Oct 18, 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.