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


-- 
Thomas Güttler, http://www.tbz-pariv.de/ 
Bernsdorfer Str. 210-212, 09126 Chemnitz, Tel.: 0371/5347-917
TBZ-PARIV GmbH  Geschäftsführer: Dr. Reiner Wohlgemuth
Sitz der Gesellschaft: Chemnitz Registergericht: Chemnitz HRB 8543

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
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