django-db-cascade-2
Installation for Django 3
pip install django-db-cascade-2
Installation for Django 2
pip install django-db-cascade-2==0.2.3
settings.py:
DATABASES = {
'default': {
'ENGINE': 'django_db_cascade.backends.postgresql_psycopg2',
# ... etc ...
}
}
Usage
from django.db import models
from django_db_cascade.fields import ForeignKey, OneToOneField
from django_db_cascade.deletions import DB_CASCADE
class Thing(Common):
account = ForeignKey('self', DB_CASCADE)
Caveats
- DB_CASCADE only supports Postgres
- DB_CASCADE does not support django on_delete signals
- DB_CASCADE will not cascade delete multiple inherited tables as expected
- DB_CASCADE will not trigger CASCADE on another model. E.g. Model A points to model B, via DB_CASCADE. Model B points to model C, via CASCADE. A will cascade delete B, B will django delete C, but deleting A will not delete C!
- DB_CASCADE on a ManyToMany of A <---> B, only A_B set records will be cascade deleted (deleting A will not delete B)
How it works
- Minimal subclassing of the django postgresql backend and the django ForeignKey field
- Added a new possible value for ForeignKey's on_delete kwarg, called DB_CASCADE
- When you use DB_CASCADE, the migration framework will recognize a change, and write new sql
- example SQL generated:
ALTER TABLE mytable ADD CONSTRAINT myconstraint FOREIGN KEY (mycolumn) REFERENCES myothertable myothercolumn ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
Future proof
If, and when, DB_CASCADE ever gets into django, editing these generated migrations should be very easy.
Generated migrations:
migrations.AlterField(
model_name='modelname',
name='fieldname',
field=django_db_cascade.fields.ForeignKey(on_delete=django_db_cascade.deletions.DB_CASCADE)
)
Changing them over, if django ever handles DB_CASCADE natively, might look like:
migrations.AlterField(
model_name='modelname',
name='fieldname',
field=django.db.models.ForeignKey(on_delete=models.DB_CASCADE)
)
Ticket
The ticker where django has discussed bringing DB_CASCADE to django: https://code.djangoproject.com/ticket/21961
Metadata
Release files for django-db-cascade-2 0.3.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-db-cascade-2-0.3.5.tar.gz | 5.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_db_cascade_2-0.3.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.9 kB
Release files / django-db-cascade-2-0.3.5.tar.gz
| Download URL | django-db-cascade-2-0.3.5.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
00925d5ce0a7a19f94619d403d077aa47784bf726ce88cf4d01db5f0ae7babce
|
|
BLAKE2b-256 checksum How to use checksums |
f6f0ba65920c0be41f85c93298f6a19753e48541d46c2ac39a3fd2e9a9e1d6ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.13
|
Release files / django_db_cascade_2-0.3.5-py3-none-any.whl
| Download URL | django_db_cascade_2-0.3.5-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eeef1b789c697f04eae1fde981d67ee0b57da052f1d8007abbb00e22d63c8187
|
|
BLAKE2b-256 checksum How to use checksums |
f6e6f792ca804a3c762daad34716568ff95a3ba5c21b3ef8865507e4b7fe97ed
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.13
|