Django:删除关系结束后未触发m2m_changed [英] Django: m2m_changed not fired when end of relation is deleted

查看:56
本文介绍了Django:删除关系结束后未触发m2m_changed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:由于生产环境的限制,我暂时必须坚持使用django-1.4.

NOTICE: due to production environment constraints, I must stick to django-1.4 for the moment.

我刚刚进行了一项测试,以查看在ManyToMany发生更改时是否可以挂接到事件上.

I've just made a test to see whether I can hook onto an event when ManyToMany changes.

我有一个Group模型,其中包含几个Item对象.每当项目在任何组中更改时,我都想对相关的Group`实例进行处理.

I have a Group model that holds several Item objects. Whenever the items change in any group, I want to do something with concerned Group` instances.

from django.db import models
    from django.db.models.signals import m2m_changed, post_delete, pre_delete

class Item(models.Model):
    name = models.CharField(max_length=32)

    def __str__(self):
        return self.name

class Group(models.Model):
    name = models.CharField(max_length=32)
    items = models.ManyToManyField(to=Item)    

def items_changed(signal, sender, action, instance, reverse, model, pk_set, using, **kwargs):
    print str(signal)
    print action, instance, reverse, pk_set

m2m_changed.connect(items_changed, sender=Group.items.through)

如果我更改Group实例上的items列表,显然会触发m2m_changed事件.

If I change the items list on a Group instance, I the m2m_changed event is obviously fired.

>>> from sandbox.core.models import Group, Item
>>> item, created = Item.objects.get_or_create(name='f')
>>> g = Group.objects.get(pk=1)
>>> g.items.add(item)
pre_add Group object False set([5])
post_add Group object False set([5])
>>> g.items.remove(item)
pre_remove Group object False set([5])
post_remove Group object False set([5])

现在,当我删除关系的Item末端时,什么也没发生,但是,关系表条目已正确删除.

Now, when I remove the Item end of the relation, nothing happens, however, the relation table entry is correctly deleted.

>>> item.delete()

我尝试通过表连接到m2m的删除信号,但是显然,不会为自动关系表触发信号.

I tried connecting to the delete signal of the m2m through table, but apparently, signals are not fired for the automatic relation tables.

在我的models.py模块中这样连接:

Connected like this in my models.py module:

def group_items_pre_delete(signal, sender, instance, using, **kwargs):
    print 'pre_delete', instance

def group_items_post_delete(signal, sender, instance, using, **kwargs):
    print 'post_delete', instance

pre_delete.connect(group_items_pre_delete, sender=Group.items.through)
post_delete.connect(group_items_post_delete, sender=Group.items.through)

我现在的解决方案是手动检查更改.这些模型是通过REST API公开的,因此我可以在更新请求结束时执行此操作(但我希望通过信号将其自动化).

The solution I have for now is to check manually for changes. The models are exposed through a REST API, so I can do this when an update request ends (but I would have liked it to be automated through the signals).

在任何较新版本的Django中是否触发了那些缺失"信号?

Are those "missing" signals fired in any newer version of Django?

推荐答案

这是一个错误,尚未修复: https://code.djangoproject.com/ticket/17688

This is a bug, not fixed yet: https://code.djangoproject.com/ticket/17688

这篇关于Django:删除关系结束后未触发m2m_changed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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