gpt4 book ai didi

Django 信号 dispatch_uid

转载 作者:行者123 更新时间:2023-12-02 01:00:35 26 4
gpt4 key购买 nike

我对 dispatch_uid 的用法有疑问为信号。

目前,我通过简单地添加 if not instance.order_reference 来防止信号的多次使用。 .我现在想知道是否dispatch_uid具有相同的功能,我可以删除“if not ”子句。

信号.py

def reserveditem_create_order_reference(sender, instance, **kwargs):
if not instance.order_reference:
instance.order_reference = unique_order_reference_generator()

app.py

class OrdersConfig(AppConfig):
name = 'orders'

def ready(self):

#Pre save signal for ReservedItem model
reserved_model = self.get_model('ReservedItem')
pre_save.connect(
reserveditem_create_order_reference,
sender=reserved_model,
dispatch_uid="my_unique_identifier"
)

最佳答案

正如 the docs ,

In some circumstances, the code connecting receivers to signals may run multiple times. This can cause your receiver function to be registered more than once, and thus called multiple times for a single signal event. If this behavior is problematic (such as when using signals to send an email whenever a model is saved), pass a unique identifier as the dispatch_uid argument to identify your receiver function



所以是的,删除 if条款和设置独特的信号接收器 dispatch_uid相反,将阻止您的处理程序被多次连接(以及此后被调用)。

关于Django 信号 dispatch_uid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50880893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com