Python Django信号.x27;功能';对象没有属性';锁';错误
我有一个功能是接收来自系统所有型号的信号。当我通过Django命令进行数据库转储填充时,我希望信号被中断,这样在通过这种方式创建模型实例时就不会调用它 这是我的接收器功能:Python Django信号.x27;功能';对象没有属性';锁';错误,python,django,django-signals,Python,Django,Django Signals,我有一个功能是接收来自系统所有型号的信号。当我通过Django命令进行数据库转储填充时,我希望信号被中断,这样在通过这种方式创建模型实例时就不会调用它 这是我的接收器功能: @receiver(post_save) def trigger_payment(sender, instance=None, created=False, **kwargs): from TreasuryManagementApp.models import PaymentApplicationTrigger
@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()
问题是对的调用发出信号。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'
回溯(最近一次呼叫最后一次):
文件“manage.py”,第15行,在
从命令行(sys.argv)执行命令
文件“/Users/hugovillalobos/Documents/Code/TaxistasProject/TaxistasVenv/lib/python3.7/site-packages/django/core/managemen
t/uuuu init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu.py”,第381行,从命令行执行
utility.execute()
文件“/Users/hugovillalobos/Documents/Code/TaxistasProject/TaxistasVenv/lib/python3.7/site-packages/django/core/managemen
t/_init__uuu.py”,执行中的第375行
self.fetch_命令(子命令)。从_argv(self.argv)运行_
文件“/Users/hugovillalobos/Documents/Code/TaxistasProject/TaxistasVenv/lib/python3.7/site-packages/django/core/managemen
t/base.py”,第316行,从argv开始运行
self.execute(*args,**cmd_选项)
文件“/Users/hugovillalobos/Documents/Code/TaxistasProject/taxistas/GeneralApp/management/commands/startdatabase.py”,林
E21,执行中
调用命令(“导入人员”)
文件“/Users/hugovillalobos/Documents/Code/TaxistasProject/TaxistasVenv/lib/python3.7/site-packages/django/core/managemen
t/uuu init_uuuu.py”,第148行,在call_命令中
return命令。execute(*args,**默认值)
文件“/Users/hugovillalobos/Documents/Code/taxistas项目/taxistas/GeneralApp/management/commands/import_people.py”,林
E433,执行中
信号断开(触发付款)
文件“/Users/hugovillalobos/Documents/Code/TaxistasProject/TaxistasVenv/lib/python3.7/site-packages/django/dispatch/dispa
tcher.py”,第138行,断开连接
使用self.lock:
AttributeError:“函数”对象没有属性“锁”
使用post\u save
而不是信号
。中的文档说明:“所有信号都是django.dispatch.Signal实例”,因此,您必须断开或连接实例,而不是Signal
类
从django.db.models.signals导入后保存
类命令(BaseCommand):
help=389;(“运行此命令以从平面文件”“导入伙伴和运算符”)
def执行(self、*args、**kwargs):
从TreasuryManagementApp.controllers导入触发付款
后保存。断开连接(触发付款)
self.import_partners()
self.import_操作符()
self.import\u dead\u ones()
post_save.connect(触发付款)#别忘了重新连接