I need make a save with a model but i need disconnect some receivers of the signals before save it.
我需要使用模型进行保存但我需要在保存之前断开信号的某些接收器。
I mean,
我的意思是,
I have a model:
我有一个模特:
class MyModel(models.Model):
...
def pre_save_model(sender, instance, **kwargs):
...
pre_save.connect(pre_save_model, sender=MyModel)
and in another place in the code i need something like:
在代码中的另一个地方我需要类似的东西:
a = MyModel()
...
disconnect_signals_for_model(a)
a.save()
...
reconnect_signals_for_model(a)
Because i need in this case, save the model without execute the function pre_save_model.
因为在这种情况下我需要保存模型而不执行函数pre_save_model。
5 个解决方案
#1
27
For a clean and reusable solution, you can use a context manager:
对于干净且可重用的解决方案,您可以使用上下文管理器:
class temp_disconnect_signal():
""" Temporarily disconnect a model from a signal """
def __init__(self, signal, receiver, sender, dispatch_uid=None):
self.signal = signal
self.receiver = receiver
self.sender = sender
self.dispatch_uid = dispatch_uid
def __enter__(self):
self.signal.disconnect(
receiver=self.receiver,
sender=self.sender,
dispatch_uid=self.dispatch_uid,
weak=False
)
def __exit__(self, type, value, traceback):
self.signal.connect(
receiver=self.receiver,
sender=self.sender,
dispatch_uid=self.dispatch_uid,
weak=False
)
Now, you can do something like the following:
现在,您可以执行以下操作:
from django.db.models import signals
from your_app.signals import some_receiver_func
from your_app.models import SomeModel
...
kwargs = {
'signal': signals.post_save,
'receiver': some_receiver_func,
'sender': SomeModel,
'dispatch_uid': "optional_uid"
}
with temp_disconnect_signal(**kwargs):
SomeModel.objects.create(
name='Woohoo',
slug='look_mom_no_signals',
)
Note: If your signal handler uses a dispatch_uid
, you MUST use the dispatch_uid
arg.
注意:如果信号处理程序使用dispatch_uid,则必须使用dispatch_uid arg。
#2
18
You can connect and disconnect signals as Haystack does in RealTimeSearchIndex, which seems more standard:
您可以像Haystack在RealTimeSearchIndex中那样连接和断开信号,这看起来更标准:
from django.db.models import signals
signals.pre_save.disconnect(pre_save_model, sender=MyModel)
a.save()
signals.pre_save.connect(pre_save_model, sender=MyModel)
#3
8
I haven't tested the following code, but it should work:
我没有测试以下代码,但它应该工作:
from django.db.models.signals import pre_save
def save_without_the_signals(instance, *args, **kwargs):
receivers = pre_save.receivers
pre_save.receivers = []
new_instance = instance.save(*args, **kwargs)
pre_save.receivers = receivers
return new_instance
It will silence signals from all sender's though not just instance.__class__
.
它会使所有发送者的信号静音,但不仅仅是实例.__ class__。
This version disables only the given model's signals:
此版本仅禁用给定模型的信号:
from django.db.models.signals import pre_save
from django.dispatch.dispatcher import _make_id
def save_without_the_signals(instance, *args, **kwargs):
receivers = []
sender_id = _make_id(instance.__class__)
for index in xrange(len(self.receivers)):
if pre_save.receivers[index][0][1] == sender_id:
receivers.append(pre_save.receivers.pop(index))
new_instance = instance.save(*args, **kwargs)
pre_save.receivers.extend(receivers)
return new_instance
#4
8
If you only want disconnect and reconnect one custom signal, you may use this code:
如果您只想断开连接并重新连接一个自定义信号,则可以使用以下代码:
def disconnect_signal(signal, receiver, sender):
disconnect = getattr(signal, 'disconnect')
disconnect(receiver, sender)
def reconnect_signal(signal, receiver, sender):
connect = getattr(signal, 'connect')
connect(receiver, sender=sender)
In this way you can make this:
通过这种方式你可以做到这一点:
disconnect_signal(pre_save, pre_save_model, MyModel)
a.save()
reconnect_signal(pre_save, pre_save_model, MyModel)
#5
0
I needed to prevent certain signals from firing during unittests so I made a decorator based on qris's response:
我需要在单元测试期间防止某些信号被触发,所以我根据qris的响应做了一个装饰器:
from django.db.models import signals
def prevent_signal(signal_name, signal_fn, sender):
def wrap(fn):
def wrapped_fn(*args, **kwargs):
signal = getattr(signals, signal_name)
signal.disconnect(signal_fn, sender)
fn(*args, **kwargs)
signal.connect(signal_fn, sender)
return wrapped_fn
return wrap
Using it is simple:
使用它很简单:
@prevent_signal('post_save', my_signal, SenderClass)
def test_something_without_signal(self):
# the signal will not fire inside this test
#1
27
For a clean and reusable solution, you can use a context manager:
对于干净且可重用的解决方案,您可以使用上下文管理器:
class temp_disconnect_signal():
""" Temporarily disconnect a model from a signal """
def __init__(self, signal, receiver, sender, dispatch_uid=None):
self.signal = signal
self.receiver = receiver
self.sender = sender
self.dispatch_uid = dispatch_uid
def __enter__(self):
self.signal.disconnect(
receiver=self.receiver,
sender=self.sender,
dispatch_uid=self.dispatch_uid,
weak=False
)
def __exit__(self, type, value, traceback):
self.signal.connect(
receiver=self.receiver,
sender=self.sender,
dispatch_uid=self.dispatch_uid,
weak=False
)
Now, you can do something like the following:
现在,您可以执行以下操作:
from django.db.models import signals
from your_app.signals import some_receiver_func
from your_app.models import SomeModel
...
kwargs = {
'signal': signals.post_save,
'receiver': some_receiver_func,
'sender': SomeModel,
'dispatch_uid': "optional_uid"
}
with temp_disconnect_signal(**kwargs):
SomeModel.objects.create(
name='Woohoo',
slug='look_mom_no_signals',
)
Note: If your signal handler uses a dispatch_uid
, you MUST use the dispatch_uid
arg.
注意:如果信号处理程序使用dispatch_uid,则必须使用dispatch_uid arg。
#2
18
You can connect and disconnect signals as Haystack does in RealTimeSearchIndex, which seems more standard:
您可以像Haystack在RealTimeSearchIndex中那样连接和断开信号,这看起来更标准:
from django.db.models import signals
signals.pre_save.disconnect(pre_save_model, sender=MyModel)
a.save()
signals.pre_save.connect(pre_save_model, sender=MyModel)
#3
8
I haven't tested the following code, but it should work:
我没有测试以下代码,但它应该工作:
from django.db.models.signals import pre_save
def save_without_the_signals(instance, *args, **kwargs):
receivers = pre_save.receivers
pre_save.receivers = []
new_instance = instance.save(*args, **kwargs)
pre_save.receivers = receivers
return new_instance
It will silence signals from all sender's though not just instance.__class__
.
它会使所有发送者的信号静音,但不仅仅是实例.__ class__。
This version disables only the given model's signals:
此版本仅禁用给定模型的信号:
from django.db.models.signals import pre_save
from django.dispatch.dispatcher import _make_id
def save_without_the_signals(instance, *args, **kwargs):
receivers = []
sender_id = _make_id(instance.__class__)
for index in xrange(len(self.receivers)):
if pre_save.receivers[index][0][1] == sender_id:
receivers.append(pre_save.receivers.pop(index))
new_instance = instance.save(*args, **kwargs)
pre_save.receivers.extend(receivers)
return new_instance
#4
8
If you only want disconnect and reconnect one custom signal, you may use this code:
如果您只想断开连接并重新连接一个自定义信号,则可以使用以下代码:
def disconnect_signal(signal, receiver, sender):
disconnect = getattr(signal, 'disconnect')
disconnect(receiver, sender)
def reconnect_signal(signal, receiver, sender):
connect = getattr(signal, 'connect')
connect(receiver, sender=sender)
In this way you can make this:
通过这种方式你可以做到这一点:
disconnect_signal(pre_save, pre_save_model, MyModel)
a.save()
reconnect_signal(pre_save, pre_save_model, MyModel)
#5
0
I needed to prevent certain signals from firing during unittests so I made a decorator based on qris's response:
我需要在单元测试期间防止某些信号被触发,所以我根据qris的响应做了一个装饰器:
from django.db.models import signals
def prevent_signal(signal_name, signal_fn, sender):
def wrap(fn):
def wrapped_fn(*args, **kwargs):
signal = getattr(signals, signal_name)
signal.disconnect(signal_fn, sender)
fn(*args, **kwargs)
signal.connect(signal_fn, sender)
return wrapped_fn
return wrap
Using it is simple:
使用它很简单:
@prevent_signal('post_save', my_signal, SenderClass)
def test_something_without_signal(self):
# the signal will not fire inside this test