django-postgres-extra
django-postgres-extra brings advanced PostgreSQL features like HStore fields, partitioned models, and enhanced querying capabilities directly into Django's ORM. It seamlessly integrates with Django models and querysets, allowing developers to leverage PostgreSQL-specific functionality without writing raw SQL. The current stable version is 2.0.9, with a 3.0.0 release candidate recently published, indicating an active development and release cadence.
Warnings
- breaking Version 3.0.0rc1 (and likely future 3.0.0 stable) introduces significant changes, including support for Django 5.2+ and Python 3.13, and integrates with Django 5.2's `CompositePrimaryKey` for partitioned models. Users upgrading from 2.x to 3.x, especially those using partitioned models, should review the changelog carefully for potential breaking changes in model definitions and behavior.
- gotcha Automatic `hstore` extension creation was broken for Django 4.2 or newer in `django-postgres-extra` versions prior to `2.0.9rc11` (and thus prior to `2.0.9`). If you are on an older version of `django-postgres-extra` (e.g., 2.0.8 or earlier) and using Django 4.2+, you might encounter issues with HStoreField functionality.
- gotcha Like most Django apps, `django-postgres-extra` must be added to your `INSTALLED_APPS` setting in `settings.py`. Failing to do so will prevent its features from being registered with Django and migrations from being generated or applied correctly.
- gotcha Utilizing advanced PostgreSQL features like partitioned models, especially with custom primary keys or complex partitioning strategies, adds complexity to your application's architecture. Schema changes, migrations, and ensuring data integrity require careful planning and understanding of PostgreSQL's partitioning mechanics.
Install
-
pip install django-postgres-extra -
pip install django-postgres-extra==3.0.0rc1
Imports
- HStoreField
from pg_extra.fields import HStoreField
- PostgreSQLPartitionedModel
from pg_extra.partitioned_models import PostgreSQLPartitionedModel
from pg_extra.models import PostgreSQLPartitionedModel
- PostgreSQLQuerySet
from pg_extra.query import PostgreSQLQuerySet
Quickstart
import os
from django.conf import settings
from django.db import models
# Minimal Django setup (for demonstration only)
if not settings.configured:
settings.configure(
INSTALLED_APPS=['pg_extra', __name__],
DATABASES={
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('PG_DB_NAME', 'test_db'),
'USER': os.environ.get('PG_DB_USER', 'user'),
'PASSWORD': os.environ.get('PG_DB_PASSWORD', 'password'),
'HOST': os.environ.get('PG_DB_HOST', 'localhost'),
'PORT': os.environ.get('PG_DB_PORT', '5432')
}
},
DEFAULT_AUTO_FIELD='django.db.models.BigAutoField'
)
from pg_extra.fields import HStoreField
class Product(models.Model):
name = models.CharField(max_length=255)
properties = HStoreField(blank=True, null=True, default=dict)
def __str__(self):
return self.name
class Meta:
app_label = 'my_app' # Required for this minimal setup
# Example usage (requires a configured Django project and migrations applied)
# print('Ensure pg_extra is in INSTALLED_APPS and run migrations.')
# try:
# p = Product.objects.create(name='Smartphone', properties={'color': 'black', 'storage_gb': '128'})
# print(f"Created product: {p.name} with properties {p.properties}")
# retrieved_p = Product.objects.get(name='Smartphone')
# print(f"Retrieved product color: {retrieved_p.properties['color']}")
# except Exception as e:
# print(f"Error during quickstart: {e}")