I've settled on a solution for this now which works fine. So I'm
overriding the delete function for my model. In that function I'm
clearing any references that I need to but I'm then also calling
delete manually on any child objects that I also have overridden
delete functions for. This does require an extra step to manually call
delete again on child objects but at least it means I can have delete
logic in a sensible place.

Also found this article on the net which details the options for this
problem nicely
http://stackoverflow.com/questions/2475249/what-are-the-options-for-overriding-djangos-cascading-delete-behaviour




On Apr 13, 7:49 pm, cootetom <coote...@gmail.com> wrote:
> OK I've tried the pre_delete approach but unfortunately it doesn't
> work for what I'm trying here. It appears that django gets a
> collection of objects that it is going to delete then it calls
> pre_delete on each object before finally deleting the objects. So
> django has decided which objects to delete way before the pre_delete
> function is called meaning if I do a clear inside that function it
> makes no difference to the list of objects that will be deleted.
>
> At the moment the only approach I can think of is to define functions
> for deleting models and do the clear logic before calling delete. This
> seems really ugly to me but I can't see any other way.
>
> def deleteModal2(obj):
>     obj.related_model.clear()
>     obj.delete()
>
> def deleteModal1(obj):
>     for childObj in obj.related_model.all():
>         deleteModal2(childObj)
>
>     obj.related_model.clear()
>     obj.delete()
>
> Any more ideas?
>
> On Apr 13, 9:29 am, cootetom <coote...@gmail.com> wrote:
>
>
>
> > Thanks Ian,
>
> > I'll give that a go later. I don't suppose it matters what order they
> > are called in because it's only clearing references to do with the
> > model instance it's calling from.
>
> > On Apr 13, 2:15 am, Ian Lewis <ianmle...@gmail.com> wrote:
>
> > > Tom,
>
> > > You could try doing this clear logic in a pre_delete signal. You might 
> > > have
> > > to test out the timing of when the signals get called but it should call
> > > pre_delete for all deleted models. In this case it would call pre_delete 
> > > on
> > > model2 before model1 but you should be able to get what you are looking 
> > > for.
>
> > >http://docs.djangoproject.com/en/dev/ref/signals/#pre-delete
>
> > > On Tue, Apr 13, 2010 at 8:21 AM, cootetom <coote...@gmail.com> wrote:
> > > > Hi, I'm trying to figure out the way django deletes models so that I
> > > > can clear the correct references that I need to prior to deleting. So
> > > > I have models set up with overrided delete functions so that I can do
> > > > clears before the actual delete. However, it appears the delete
> > > > functions in a model don't get called in the cascade of deletes so not
> > > > each child model gets to do it's clear of linked data before
> > > > deleting.
>
> > > > def model1(models.Model):
> > > >    def delete(self):
> > > >        self.related_model.clear()
> > > >        super(model1, self).delete()
>
> > > > def model2(models.Model):
> > > >    model2 = models.ForeignKey(model2)
>
> > > >    def delete(self):
> > > >        self.another_related_model.clear()
> > > >        super(model2, self).delete()
>
> > > > So if I do model1.delete() then it will do it's clear but it appears
> > > > it won't do the clear from model2? Am I getting this behaviour right
> > > > or am I doing something wrong here?
>
> > > > The model class seems like the best place to put delete logic in so
> > > > that when it's deleted it clears any data it needs to first.
>
> > > > - Tom
>
> > > > --
> > > > You received this message because you are subscribed to the Google 
> > > > Groups
> > > > "Django users" group.
> > > > To post to this group, send email to django-us...@googlegroups.com.
> > > > To unsubscribe from this group, send email to
> > > > django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@google
> > > >  groups.com>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/django-users?hl=en.
>
> > > --
> > > =======================================
> > > 株式会社ビープラウド  イアン・ルイス
> > > 〒150-0021
> > > 東京都渋谷区恵比寿西2-3-2 NSビル6階
> > > email: ianmle...@beproud.jp
> > > TEL:03-6416-9836
> > > FAX:03-6416-9837http://www.beproud.jp/
> > > =======================================

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