Django: Enumerate Objects that Will Get Cascade-Deleted
When you try to delete an object, the Django admin gives you a list of all the related objects that will be cascade-deleted as a result. What if you want to get a list of these objects programmatically? You could do it this way:
from django.contrib.admin.util import NestedObjects
from django.db import DEFAULT_DB_ALIAS
obj = ... # the object you are going to delete
collector = NestedObjects(using=DEFAULT_DB_ALIAS)
collector.collect([obj])
print collector.nested()
This gives you a nested list of the objects that will get deleted if you delete
obj. Note that NestedObjects is an undocumented API, so there is no
guarantee that it will continue working from version to version. I tested only
with Django 1.4.1.