Skip to main content
django-postgres-denormalize

django-postgres-denormalize

PyPI Supported Python versions Supported Django versions Supported PostgreSQL versions CI Documentation Coverage BSD-3-Clause licence

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.2

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-postgres-denormalize 1.0.2
File Size Uploaded Provenance Actions
django_postgres_denormalize-1.0.2.tar.gz 174.9 kB With provenance Details for django_postgres_denormalize-1.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-postgres-denormalize 1.0.2
File Interpreter ABI Platform Actions
django_postgres_denormalize-1.0.2-py3-none-any.whl Python 3 none any Details for django_postgres_denormalize-1.0.2-py3-none-any.whl

Total release size: 216.6 kB

Release files / django_postgres_denormalize-1.0.2.tar.gz

Download URL django_postgres_denormalize-1.0.2.tar.gz
Size 174.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2d4975fd68156ae3c961590913b0de1def855fbf8d0fcbb7e47855249efaf599
BLAKE2b-256 checksum
How to use checksums
91288cc139b7cb1da060dffb401d7dc2e42c821010d0cb5fb4ac6fd7bc46aa6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 9, 2026.

Transparency log

Release files / django_postgres_denormalize-1.0.2-py3-none-any.whl

Download URL django_postgres_denormalize-1.0.2-py3-none-any.whl
Size 41.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c6d2b03b363e193778cf92bb747224c84aafae0db8c3315863823e77112871fc
BLAKE2b-256 checksum
How to use checksums
a8b6d2330b13a4e3493b7b71a8d7e9087b8bbfade663984a0994d8688cda57a9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Nov 9, 2026.

Transparency log

Security for django-postgres-denormalize 1.0.2

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.

Select from 2 files

Attestation for django_postgres_denormalize-1.0.2.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 Nov 9, 2026.

Transparency log

A 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.

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