一个信号m2m_changed和bug与post_remove [英] A signal m2m_changed and bug with post_remove

查看:140
本文介绍了一个信号m2m_changed和bug与post_remove的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检测一个post_remove信号,所以我写了:

I need to detect a post_remove signal, so I have written :

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_remove'由'post_add'它是好的..它是一个django的错误关于post_remove?

If I change 'post_remove' by 'post_add' it is ok.. Is it a django's bug about post_remove ??

我使用该模型,我切换两个值订阅 (所以添加一个和删除)

I use that model and I switch beetween two values of 'subscribed' (so one added and one deleted)

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错误的帖子,也许没有被修复(或者是我^^ )

I have seen a post with a bug of django, maybe it havn't been fixed... (or it's me ^^)

推荐答案

据我所知,这不是一个错误,只是Django不按照您期望的方式更新m2m关系。它不会删除要删除的关系,然后添加新的。相反,它清除所有的m2m关系,然后再次添加它们。

As I understand it, it's not a bug, it's just that Django does not update m2m relations in the way you expect. It does not remove the relations to be deleted then add the new ones. Instead, it clears all of the m2m relations, then adds them again.

有一个相关的问题 Django信号m2m_changed未触发链接到机票 13087

There's a related question Django signal m2m_changed not triggered which links to ticket 13087.

所以你可以使用<$ c检查 pre_clear post_clear $ c> m2m_changed 信号,但由于这些操作不提供 pk_set ,它不能帮助您在保存之前找到相关条目,如您希望在您的其他问题中执行。

So you can check for the pre_clear or post_clear action with the m2m_changed signal, but since those actions do not provide pk_set, it doesn't help you find the related entries before the save, as you wanted to do in your other question.

这篇关于一个信号m2m_changed和bug与post_remove的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆