The collection of backports for the Django v2.x using modern PostgreSQL features
Latest version from the GIT repository:
pip install "git+git://github.com/nnseva/django2-postgres-backport.git"
Stable version from the PyPi repository:
pip install django2-postgres-backport
The modern versions of the psycopg
package allow using modern PostgreSQL database versions.
The patch makes the Django v2.x code compatible with the modern psycopg
package versions.
Note the PostGIS
part of the Django v2.x requires additional forces (not yet implemented) to be compatible with the database.
Use the following code in settings.py
to patch the Django v2.x code:
from django2_postgres import psycopg_patch
psycopg_patch.fix()
The patch code is version-dependent and just does nothing for the modern Django versions.
Introduce running Add and Remove PostgreSQL indexes concurrently as it is available in the Django version >= 3
Use the modern syntax in your model to declare indexes, for example:
class MyModel(models.Model):
field1 = models.CharField(max_length=10)
field2 = models.charField(max_length=10)
class Meta:
indexes = [
models.Index(fields=['field1', 'field2'], name='my_index'),
]
Create a migration as usual, for example:
python manage.py makemigrations mypackage
Modify a migration to use the CREATE INDEX CONCURRENTLY manually.
- import the backport
- mark the migration as non-atomic
- replace the
AddIndex
by theAddIndexConcurrently
- replace the
RemoveIndex
by theRemoveIndexConcurrently
For example:
# Generated by Django 2.2.5 on 2025-05-26 13:51
from django.db import migrations, models
# -- import the backport
from django2_postgres import operations
class Migration(migrations.Migration):
dependencies = [
('tests', '0001_initial'),
]
# -- The concurrent index creation can be only in non-atomic migration
atomic = False
operations = [
# -- We remove the originally generated operation ...
# migrations.AddIndex(
# model_name='mymodel',
# index=models.Index(fields=['field1', 'field2'], name='my_index'),
# ),
# -- And replace it by a modified one
operations.AddIndexConcurrently(
model_name='mymodel',
index=models.Index(fields=['field1', 'field2'], name='my_index'),
),
]
Using concurrent index creation has its own side effects described in the Django documentation, and in the PostgreSQL documentation, please read it carefully.
The backport doesn't add any additional side effects.
The modern Django versions (>=3.0) have originally provided versions of these functions.
The backport files are version-dependent and provide original Django operations instead of backported, when the version of the Django is upgraded. Therefore, you can use all your old migrations without any changes when transfer your code to the new Django version.