开发者

Django pre_delete and post_delete signals are not synchronous when using clear on intermediate models

开发者 https://www.devze.com 2023-03-25 19:39 出处:网络
I have 3 models class A(models.Model): ...some fields.. class B(models.Model): a = models.ManyToManyField(A, through=\'C\')

I have 3 models

class A(models.Model):
   ...some fields..

class B(models.Model):
   a = models.ManyToManyField(A, through='C')

class C(models.Model):
   a = models.ForeignKey(A)
   b = models.FoeignKey(B)
   ...some extra fields..

I am using django signals to do some calculations whenever the relationship is changed. Since, there is an explicit Intermediate model specified, I can't use m2m_changed for removal. So I was using pre_delete. It's all fine except when clear() is called on any side of the relationship. Calling clear() is calling delete on intermediate model, which is firing pre_delete and post_delete. I tried checking the order of deletes, it'll fire all the pre_deletes first and then all the post_deletes.

For eg. If there are instances of C (c1, c2, c3) which will be deleted as a result of开发者_StackOverflow社区 clear(). The order is --

 pre_delete c1
 pre_delete c2
 pre_delete c3
 post_delete c3
 post_delete c2
 post_delete c1

During pre_delete of c2, c1 is still present; it is bungling up the calculations. I am not using m2m_changed pre_clear/post_clear because I want to hook to individual deletes of C, which will be called on clear() as well. Any suggestions here? Django signals documentation is also not very explanatory.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号