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()

This works if I explicitly delete a book (ie, by "works", I mean that
the book's readers stay around).  However, if I delete a publisher, it
seems that during Model::delete(), when _collect_sub_objects() runs,
it runs through all of the related objects finds the related books,
and then runs through the books' related objects and find the related
readers.  It targets them for deletion and then at some point they get
deleted.  It seems to me (I don't fully grock this code, so I could be
wrong here)  that by the time my Book::delete() code runs, the readers
of the book have already been targeted for deletion and the
self.reader_set.clear() is not having the effect I want.   So I'm
trying to figure out how I should avoid the readers getting deleted in
this case?

Thanks for any insights!

Margie

--~--~---------~--~----~------------~-------~--~----~
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