This abstract base class likely is a bit less performant than having
in-db support for cascades in pgsql... though it should give you the
behavior you are seeking.

It should set to default, or null if allowed.

Enjoy.

-k

class ClearOnDelete(models.Model):
   def delete(self):
       related_objects = self._meta.get_all_related_objects()
       for object in related_objects:
           accessor_name = object.get_accessor_name()
           related_accessor = getattr(self.__class__, accessor_name)
           related_accessor_instance = getattr(self, accessor_name)

           if related_accessor.related.field.default is not NOT_PROVIDED:
               for relation in related_accessor_instance.all():
                   setattr(relation,
related_accessor.related.field.name,
related_accessor.related.field.default)
                   relation.save()

           elif related_accessor.related.field.null:
               for relation in related_accessor_instance.all():
                   setattr(relation, related_accessor.related.field.name, None)
                   relation.save()
       super(ClearOnDelete, self).delete()

   class Meta:
       abstract = True

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to