Skip to main content
django-postgres-denormalize

django-postgres-denormalize

PyPI Supported Python versions Supported Django versions Supported PostgreSQL versions CI Documentation 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.1

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.1
File Size Uploaded Provenance
django_postgres_denormalize-1.0.1.tar.gz 174.5 kB With provenance Details

Built distribution (wheel)

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

Total release size: 216.3 kB

Release files / django_postgres_denormalize-1.0.1.tar.gz

Download URL django_postgres_denormalize-1.0.1.tar.gz
Size 174.5 kB
Tags Source
SHA-256 checksum
How to use checksums
76d683463f6be34bb8a93bb4232397c76dfc3ccb7da7e8367d67c1f017765ccb
BLAKE2b-256 checksum
How to use checksums
40afc86144d9d3342a567c046a8c0f69e82e765e6e9ed6f2958b57cd23dbb241
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 19, 2026.

Transparency log

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

Download URL django_postgres_denormalize-1.0.1-py3-none-any.whl
Size 41.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ae5b6d6035ad94b75b348cb6069838b6ceccf683baf5400bf4dbb8254144eb4b
BLAKE2b-256 checksum
How to use checksums
44c3d1c3fac447faee9ebc1fad9324324c7542acc5414eee9df3ac72a735865a
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 19, 2026.

Transparency log

Security for django-postgres-denormalize 1.0.1

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.

Select from 2 files

Attestation for django_postgres_denormalize-1.0.1.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 19, 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.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files Full provenance

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