I see. So then I think what you are saying is that if I want to avoid these "readers" getting deleted, prior to deleting my publisher I need to find all readers of the books publised by that publisher, and clear them out so that they are no longer readers of those books.
Let's say that my Publisher and Book classes are in one app, and that app doesn't know anything about the readers. Is there any simple way to find all related object fields that point to book and clear them out, without having to know their names? One other thought - let me know if you see any issue with this. Let's say I just never want my reader objects to get deleted based on related object deletions. I think I can just define class Reader: def delete(self): pass Then delete will do nothing and if I really want to delete my reader I can call some other method that I define that then calls super(Reader, self).delete(). The actual object that is my "reader" (the above example was used just for simplicity) is really my UserProfile object. I would think that this is a common problem - not wanting one's userProfiles to ever get deleted. In my case my UserProfile contains a ForeignKey to a particular "Chip" object, identifying it as the "current chip" (ie, the one they want to be looking at). If that Chip gets deleted, I don't want my UserProfile to get deleted. As I write this, it occurs to me that another option would be to make the UserProfile contain a manytomany field that identifies the current chip (rather than a foreingKey). I think if I did that, I would not have this problem, right? Is there a down side to making it a ManyToMany field rather than a ForeignKey? It's misleading (since there can be only one current chip pointed at by the userprofile), but it seems to solve the problem. Margie On Apr 12, 12:06 am, Malcolm Tredinnick <malc...@pointy-stick.com> wrote: > 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-que... > > (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 -~----------~----~----~----~------~----~------~--~---