Support of model_object.delete(cascade=True)

65 views
Skip to first unread message

Sven R. Kunze

unread,
May 27, 2016, 8:49:42 AM5/27/16
to Django developers (Contributions to Django itself)
Hi everybody,

That's my first proposal for Django, so I hope it's in the right shape. :)


Even though we have on_delete=PROTECTED on several models fields for a very good reason, there are circumstances where we want to circumvent this restriction for a very good reason, too. And we are 110% sure that this is a safe operation.

Tim already suggested to do this manually but this is very tedious for larger database structure as we have. We already wrote a generic function which provides that capability but we would appreciate a Django-bulitin functionality for this as we duplicate some Django-internal code.


Note 1: overriding Model.delete() does not help as we needed it (this time) in a backward migration to clean up what has been created in a forward migration. Migrations don't expose model-overriden functions.
Note 2: discussion started here: https://code.djangoproject.com/ticket/26664


Would this be valuable addition to Django?


Best,
Sven

charettes

unread,
May 27, 2016, 10:53:36 AM5/27/16
to Django developers (Contributions to Django itself)
Hi Sven,

I agree with Tim about the fact such an option could be dangerous as you
can't control the extent of the cascade.

We removed the depth parameter of `QuerySet.select_related()` method for
similar reasons and in this case you only ended up with a slow query not
possibly purged data.

They're have been a couple of issues reported related to cascade deletion
in the past and most of the affected users we're saved by the enforced
constraints at the database level. I'm afraid the addition of such an option
could lead unexperienced developers to always pass cascade=True when
they encounter a foreign key constraint check failure.


> overriding Model.delete() does not help as we needed it (this time) in a backward
> migration to clean up what has been created in a forward migration. Migrations
> don't expose model-overriden functions.

Could simply define this feature a as imported function that takes a model instance
or define a model mixin witht the overriden `delete(cascade)` method? The autodetector
should include it in the model bases.

Cheers,
Simon
Reply all
Reply to author
Forward
0 new messages