On Sat, 2009-04-11 at 23:29 -0700, Margie wrote:
> I am having some trouble with the deletion of related objects that are
> multiple levels away from the object being deleted.  I've read a bunch
> of stuff written on deletion of related objects, but nothing on this
> particular problem  - hoping someone can help.
> 
> Say I have a model like this:
> 
> class Publisher(models.Model):
>     name = models.CharField(max_length=100)
> 
> class Book(models.Model):
>     publisher = models.ForeignKey(Publisher, blank=True, null=True)
> 
> class Reader(models.Model):
>     book = models.ForeignKey(Book, blank=True, null=True)
> 
> When a publisher is deleted, I would its book_set to be deleted, and
> this happens by default.  However, when a book is deleted, either by
> deleting the book explicitely or due to its related publisher being
> deleted, I don't want the book's reader_set to be deleted.  In Book I
> have this:
> 
> class Book(models.Model):
>   def delete(self):
>     self.reader_set.clear()  # make any readers of the book no longer
> reference the book to be deleted
>     super(Book, self).delete()
The basic problem you're up against here is that a model's delete()
method is not necessarily going to be run when it is deleted indirectly.
Django tries to optimise deletes and updates so that they are single (or
minimal numbers of) SQL queries, which means not calling a method on
each instance, but, rather, doing a bulk delete at the database level.

This is documented, although it's one of those things that doesn't
immediately jump out:
http://docs.djangoproject.com/en/dev/topics/db/queries/#topics-db-queries-delete

(The queryset and model documentation is a bit all over the place at the
moment -- we need a few more links between places.)

I think that's really going to be the showstopper here. You can't hope
to control the bulk delete (which includes related object deletion) at
that sort of level.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to