Registry / database / django-pgviews-redux

django-pgviews-redux

JSON →
library1.2.0pypypiunverified

django-pgviews-redux is a Django library designed to manage PostgreSQL views, including materialized views, as part of your Django models and migrations. It provides tools to define SQL views in your Django applications, track their schema changes, and integrate them seamlessly into your database lifecycle. The current version is 1.2.0, with an active but irregular release cadence.

pip install django-pgviews-redux
INSTALL
IMPORT
SIG · DJANGO-PGVIEWS-RED
D
django-pgviews-redux
databasepythonv1.2.0
Install
3.1s avg
Import
Disk
56MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 66.6MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 3.1s · import 0.000s · 67MB
56MB installed
● package 56MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PostgresView
from django_pgviews import PostgresView
from django_pgviews import PostgresView

This quickstart demonstrates how to define both a regular and a materialized PostgreSQL view in a Django application. Views are defined as Django models that inherit from `PostgresView` or `MaterializedView` mixins. Remember to set `managed = False` in the `Meta` class and include an `id` field as a primary key. Materialized views require explicit refreshing to update their data.

import os from django.db import models from pgviews.view import PostgresView from pgviews.materialized_view import MaterializedView # Assuming 'myapp' is an installed Django app # Add 'pgviews' to INSTALLED_APPS in settings.py: # INSTALLED_APPS = [ # ..., # 'myapp', # 'pgviews', # ] # Define a base model for demonstration class Author(models.Model): name = models.CharField(max_length=100) def __str__(self): return self.name class Book(models.Model): title = models.CharField(max_length=200) author = models.ForeignKey(Author, on_delete=models.CASCADE) def __str__(self): return self.title # Define a regular PostgreSQL View class AuthorBookCountView(PostgresView, models.Model): # The SQL query must define an 'id' field, as Django models require a primary key. view_definition = """ SELECT a.id AS id, a.name AS author_name, COUNT(b.id) AS book_count FROM myapp_author a LEFT JOIN myapp_book b ON a.id = b.author_id GROUP BY a.id, a.name """ id = models.IntegerField(primary_key=True) author_name = models.CharField(max_length=100) book_count = models.IntegerField() class Meta: # Important: Django should not manage the creation/deletion of the view itself. managed = False db_table = 'author_book_count_view' # Explicitly set db_table for clarity # Define a Materialized PostgreSQL View class MaterializedAuthorBookCountView(MaterializedView, models.Model): view_definition = """ SELECT a.id AS id, a.name AS author_name, COUNT(b.id) AS book_count FROM myapp_author a LEFT JOIN myapp_book b ON a.id = b.author_id GROUP BY a.id, a.name """ id = models.IntegerField(primary_key=True) author_name = models.CharField(max_length=100) book_count = models.IntegerField() class Meta: managed = False db_table = 'materialized_author_book_count_view' def refresh_data(self): self.refresh() # Method provided by MaterializedView mixin # To use: # 1. Add 'pgviews' and 'myapp' to INSTALLED_APPS in settings.py. # 2. python manage.py makemigrations myapp # 3. python manage.py migrate # 4. To query: AuthorBookCountView.objects.all() # 5. To refresh materialized view: MaterializedAuthorBookCountView.objects.first().refresh_data() or MaterializedAuthorBookCountView.objects.refresh()
Debug
Known issues
breakingVersion 1.0.0 introduced significant breaking changes, including extensive codebase refactoring, removal of unused code, and conversion of some parameters to keyword-only. Review the v1.0.0 changelog thoroughly before upgrading from pre-1.0 versions.
fix
Consult the changelog and the latest documentation. Update calls to any affected functions and adjust code that relies on previous internal structures.
affects: >=1.0.0
breakingStarting with v1.0.0, the default behavior for `MATERIALIZED_VIEWS_CHECK_SQL_CHANGED` was changed from `False` to `True`. This means materialized views are now recreated from scratch when their definition changes, which can lead to data loss or performance issues if not explicitly handled or if you expected incremental updates.
fix
If you require the old behavior (attempting to detect and apply minimal changes) or have specific data retention requirements, you may need to explicitly set `MATERIALIZED_VIEWS_CHECK_SQL_CHANGED = False` in your Django settings or implement custom migration logic to handle data migration during view recreation.
affects: >=1.0.0
gotchaWhen defining views as Django models, you must include a primary key (e.g., `id = models.IntegerField(primary_key=True)`) and set `managed = False` in the model's `Meta` class. Failing to do so will cause Django ORM issues or attempt to create a table instead of a view.
fix
Ensure your view model includes `id = models.IntegerField(primary_key=True)` (or another suitable primary key) and `class Meta: managed = False`.
affects: All versions
gotchaMaterialized views are static snapshots of data at the time of their last refresh. They do not automatically update when underlying tables change. You must explicitly call the `refresh()` method or use the `pgviews.functions.refresh_materialized_view` utility.
fix
Implement a strategy for refreshing materialized views, such as periodic tasks (e.g., using Celery), database triggers, or manual calls via Django management commands, depending on your data freshness requirements.
affects: All versions
Upgrade
Version history
1.2.0latest on PyPI · released Mar 11, 2026
Audit
Dependencies
DjangorequiredCore framework dependency for Django applications.
psycopg2-binaryrequiredPostgreSQL adapter for Python, required to interact with PostgreSQL.
Agent activity
20 hits · last 30 days
node
20
Resources
django-pgviews-redux — pip install django-pgviews-redux · libregistry