Fork of django-db-cascade for Django 2.0. Optionally use postgres db ON CASCADE DELETE on django foreign keys
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django-db-cascade-2-0.3.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0faef223073e5c9ecef0c53d98433da6661fcb79f23300ff702107309d71056 |
|
MD5 | 4d01f32bc57a8e971acdbd158534a099 |
|
BLAKE2b-256 | 4a51d08ea1a1e7a5720e3bd7aac7cd95965b879b87b21e3c6d6e4d54a44a62ee |
Close
Hashes for django_db_cascade_2-0.3.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcb85d81c58229820046b327bc40ccb996d54008c9b04f5b0f25363e8403dff0 |
|
MD5 | e8445f62cccf57814cee09694d1373f2 |
|
BLAKE2b-256 | 648272cbc0615c59c54cfb7801fa9cf1b5edbda495ed5f942dd4c7d11e71e247 |