On Mon, 2007-10-22 at 15:03 +0200, Thomas Güttler wrote:
> Hi,
> 
> since obj.delete() uses "on delete cascade", I want to see first
> which instances still have a reference to obj (and would get deleted).
> 
> I write this little helper. Comments welcome:
> 
> def all_related_instances(instance):
>     """
>     Return all instances which have a relation to this instance.
>     You can check this list before instance.delete(), since all returned
>     instances would get deleted, too (ON DELETE CASCADE)
>     """
>     assert isinstance(instance, models.Model)
>     meta=instance._meta
>     related=[]
>     for related_object in 
> meta.get_all_related_objects()+meta.get_all_related_many_to_many_objects():
>         attr = getattr(instance, related_object.get_accessor_name())
>         related.extend(attr.all())
>     return related

That would seem to only account for things that are directly attached to
the current model, rather than more distant relations.

Have a look at what Model._collect_sub_objects() does -- or call it
directly -- to see how Django goes about collecting all the objects for
deletion. That method is seeded with an empty SortedDict() from
Model.delete().

Regards,
Malcoml

-- 
Depression is merely anger without enthusiasm. 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to