Proposal: Add on_delete behaviour to ManyToManyField

While it’s possible to use on_delete when using a through model, it would be convenient to be able to do so without needing to define a though model.
This is especially applicable for existing projects, where on_delete=CASCADE has become undesirable, and migrating to a through model may be a challenge due to existing data.

Looking into this, it appears it would be fairly easily achieved by modifying create_many_to_many_intermediary_model and ManyToManyField.init and adding to_on_delete and from_on_delete, which could default to CASCADE to preserve current behavior.

I think only CASCADE, PROTECT and RESTRICT would make sense, so an exception could be raised if either a through model is provided, or if SET_NULL, SET_DEFAULT or DO_NOTHING is used.

I’d love to hear your thoughts.

Django documents how to migrate to a through model and preserve data: How to create database migrations | Django documentation | Django

Since adding an on_delete parameter to the field is essentially a shortcut for adding it to the underlying through model, I don’t think it’s worth it. There’s already a way to do it, so that’s normally a good argument against expanding the surface area of Django.

(Also M2M models have two FK’s, which may want different behaviours, so we’d really need TWO parameters.)