cascade deletion happens before pre_delete signal is sent?

617 views
Skip to first unread message

msoulier

unread,
Apr 3, 2010, 10:05:08 AM4/3/10
to Django users
I have a model A, which has a ForeignKey to model B.

A ------> B

When B is deleted I want to save the As that are pointing at B by
repointing them at a different B if possible. To do this I have a
pre_delete signal registered for B models. Unfortunately does doesn't
work.

Django follows the relationship from B to A, and deletes the As before
signaling the pre_delete event for B. So, and event that could save
the As happens too late.

Now, I could use a pre_delete for A, but then I won't know that the A
is about to be deleted as a direct result of B being deleted, so I
can't possibly tell that B is about to go away, so I should move the
pointer to B to another B. This could simply be an admin directly
deleting an A.

So, signals fail me here, do they not? The only way to do this is to
override the delete method in B and take action there instead. I'd
prefer to avoid that by using signals.

Mike

Tomasz Zieliński

unread,
Apr 3, 2010, 6:23:13 PM4/3/10
to Django users

I believe that this question was asked on this group a long time ago,
and the answer was that pre_delete signal was fired *before* actual
deletion occured,
but *after* objects were collected for deletion (i.e. after list of
objects that were to be deleted
was created - you can think of this list as of "deletion tree" show in
Django admin).

Writing this, I don't know if you've observed the same problem.

--
Tomasz Zielinski
http://pyconsultant.eu

msoulier

unread,
May 5, 2010, 4:06:03 PM5/5/10
to Django users
On Apr 3, 6:23 pm, Tomasz Zieliński <tomasz.zielin...@pyconsultant.eu>
wrote:
> I believe that this question was asked on this group a long time ago,
> and the answer was that pre_delete signal was fired *before* actual
> deletion occured,
> but *after* objects were collected for deletion (i.e. after list of
> objects that were to be deleted
> was created - you can think of this list as of "deletion tree" show in
> Django admin).
>
> Writing this, I don't know if you've observed the same problem.

It seems that way. The bottom line is that pre_delete is not helping
to prevent unintended deletion. To solve this issue I've had to define
my own signal and send that in my model's delete method.

Mike

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

Reply all
Reply to author
Forward
0 new messages