有没有一种方法可以在简单的历史中断开post_delete信号



我需要为查询集中删除的每个对象bulk_create历史记录。我把它编码正确了,我想如下。

def bulk_delete_with_history(objects, model, batch_size=None, default_user=None, default_change_reason="", default_date=None):
"""
The package `simple_history` logs the deletion one object at a time.  
This does it in bulk.
"""
model_manager = model._default_manager
model_manager.filter(pk__in=[obj.pk for obj in objects]).delete()
history_manager = get_history_manager_for_model(model)
history_type = "-"
historical_instances = []
for instance in objects:
history_user = getattr(
instance,
"_history_user",
default_user or history_manager.model.get_default_history_user(
instance),
)
row = history_manager.model(
history_date=getattr(
instance, "_history_date", default_date or timezone.now()
),
history_user=history_user,
history_change_reason=get_change_reason_from_object(
instance) or default_change_reason,
history_type=history_type,
**{
field.attname: getattr(instance, field.attname)
for field in instance._meta.fields
if field.name not in history_manager.model._history_excluded_fields
}
)
if hasattr(history_manager.model, "history_relation"):
row.history_relation_id = instance.pk
historical_instances.append(row)
return history_manager.bulk_create(
historical_instances, batch_size=batch_size
)

但问题是,我需要断开post_delete信号,这样在一次完成所有操作之前,历史记录就不会由简单的历史创建。

我试过了,但没用。

models.signals.post_delete.disconnect(HistoricalRecords.post_delete, sender=Customer)

其中Customer只是我用来测试这个实用程序函数的一个类。

有人能提出建议吗?提前谢谢。

在他们的github页面上也被问到了这个问题-https://github.com/jazzband/django-simple-history/issues/717

我在断开信号时犯了一个典型的错误。当然,必须断开与连接的对象的连接。这将在这里进行更详细的讨论-django信号断开不工作

这现在起作用了,因为我正在断开与正确对象的连接。

receiver_object = models.signals.post_delete._live_receivers(Customer)[0]
models.signals.post_delete.disconnect(receiver_object, sender=Customer)

尽管如此,我认为如果django simple history提供一个bulk_delete实用程序函数,就像他们为bulk_create和bulk_update提供的一样,那就太好了。

最新更新