Django Signal.disconnect 引发 'function' object has no attribute 'lock' 错误

Django Signal.disconnect raises 'function' object has no attribute 'lock' error

我有一个功能是接收所有型号系统的信号。当我通过 Django 命令进行数据库转储填充时,我希望信号被中断,因此当通过这种方式创建模型实例时它不会被调用。

这是我的接收器函数:

@receiver(post_save)
def trigger_payment(sender, instance=None, created=False, **kwargs):
    from TreasuryManagementApp.models import PaymentApplicationTrigger

    if instance.__class__.__name__ not in trigger_models:
        return

    strategies = EventStrategies()

    application_triggers = PaymentApplicationTrigger.objects.filter(event_name__in=trigger_models[instance.__class__.__name__]['event_names'])
    for application_trigger in application_triggers:
        strategies.execute(application_trigger, instance)

这是我尝试断开功能与信号的命令代码:

class Command(BaseCommand):
    help = _("""Run this command to import partners and operators from plane files""")

    def execute(self, *args, **kwargs):
        from TreasuryManagementApp.controllers import trigger_payment

        Signal.disconnect(trigger_payment)
        self.import_partners()
        self.import_operators()
        self.import_dead_ones()

问题是对 Signals.disconnect() 的调用引发了以下异常:

Traceback (most recent call last):

  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/hugovillalobos/Documents/Code/TaxistasProject/TaxistasVenv/lib/python3.7/site-packages/django/core/managemen
t/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/hugovillalobos/Documents/Code/TaxistasProject/TaxistasVenv/lib/python3.7/site-packages/django/core/managemen
t/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/hugovillalobos/Documents/Code/TaxistasProject/TaxistasVenv/lib/python3.7/site-packages/django/core/managemen
t/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/hugovillalobos/Documents/Code/TaxistasProject/taxistas/GeneralApp/management/commands/startdatabase.py", lin
e 21, in execute
    call_command('import_people')
  File "/Users/hugovillalobos/Documents/Code/TaxistasProject/TaxistasVenv/lib/python3.7/site-packages/django/core/managemen
t/__init__.py", line 148, in call_command
    return command.execute(*args, **defaults)
  File "/Users/hugovillalobos/Documents/Code/TaxistasProject/taxistas/GeneralApp/management/commands/import_people.py", lin
e 433, in execute
    Signal.disconnect(trigger_payment)
  File "/Users/hugovillalobos/Documents/Code/TaxistasProject/TaxistasVenv/lib/python3.7/site-packages/django/dispatch/dispa
tcher.py", line 138, in disconnect
    with self.lock:
AttributeError: 'function' object has no attribute 'lock'

使用 post_save 而不是 Signalhere 中的文档说:"All signals are django.dispatch.Signal instances",因此,您必须断开或连接您的实例,而不是 Signal class.

from django.db.models.signals import post_save

class Command(BaseCommand):
    help = _("""Run this command to import partners and operators from plane files""")

    def execute(self, *args, **kwargs):
        from TreasuryManagementApp.controllers import trigger_payment

        post_save.disconnect(trigger_payment)
        self.import_partners()
        self.import_operators()
        self.import_dead_ones()
        post_save.connect(trigger_payment)  # don't forget to reconnect