我需要检测post_remove信号的错误,所以我写了:信号m2m_changed与post_remove
def handler1(sender, instance, action, reverse, model, pk_set, **kwargs):
if (action == 'post_remove'):
test1() # not declared but make a bug if it works, to detect :)
m2m_changed.connect(handler1, sender=Course.subscribed.through)
如果我用“post_add”这是确定..改变“post_remove”它是一个Django的bug关于post_remove ??
我使用的模型和我切换beetween的“订阅”两个值(所以一加,一个删除)
class Course(models.Model):
name = models.CharField(max_length=30)
subscribed = models.ManyToManyField(User, related_name='course_list', blank=True, null=True, limit_choices_to={'userprofile__status': 'student'})
我已经看到了使用Django的错误后,也许它没有带过固定...(或它是我^^)
我真的不知道该怎么办,我需要娄代码
要做...我不能使用信号,要么.save()覆盖..我需要从模型字段(M2M)中删除用户,如果他从另一个模型的字段(M2M)中删除... – nlassaux 2012-07-27 11:48:36
I'我不确定最好的方法。您可以尝试使用'pre_save'信号将实例上的相关对象存储到实例中。 'instance._old_m2m = list(instance.subscribed.values_list('pk',flat = True))'。然后在你的'post_add'信号处理器中,比较'pk_set'和'instance._old_m2m'。祝你好运! – Alasdair 2012-07-27 12:05:16
是的,我已经考虑过了,但它并没有真正优化:/我会搜索... – nlassaux 2012-07-27 12:12:35