Django pre_delete 信号被忽略
Django pre_delete signal gets ignored
这是模型文档 pre_delete 上的 运行。当按照建议的最佳实践将此代码放在单独的文件 (signals.py) 中时,它会被皇家忽略。当放入模型文件时,它工作正常。
from django.db.models.signals import pre_delete, pre_save
from django.dispatch import receiver
from _myTools.functions import ImageProcessing
from content_image.models import Document
import os
# Image deletion when deleting entire entry
@receiver(pre_delete, sender=Document, dispatch_uid='document_delete_signal')
def entry_deletion_images_delete(sender, instance, using, **kwargs):
for key, value in instance.imageSizes.items():
name_of_image_field = str(getattr(instance, key)) # Converts to string, if not is object itself
os.remove(instance.baseDir + name_of_image_field)
setattr(instance, key, None)
那么问题是什么?我应该在那里导入更多东西吗?或者我应该将此文件导入某处吗?
问题是,如果您将其放入 signals.py
(按照推荐)但什么都不做,那么就没有人会导入该文件。
您应该遵循 this advice,尤其是
In practice, signal handlers are usually defined in a signals submodule of the application they relate to. Signal receivers are connected in the ready() method of your application configuration class. If you’re using the receiver() decorator, simply import the signals submodule inside ready().
ready()
负责导入模块,触发信号"connection",让信号接收器工作
完整的程序应该是:
# my_app/__init__.py
default_app_config = 'my_app.apps.ConfigForMyApp'
而 apps.py 应该是:
# my_app/apps.py
from django.apps import AppConfig
class ConfigForMyApp(AppConfig):
# Optionally add `name` and `verbose_name`
# for the app
def ready(self): # overriding the ready method
# This will trigger the @receiver decorator
# and thus connect the signals
import my_app.signals
尽管 MariusSiuram 提供的答案是 django >= 1.7 的新推荐方式,但这里是另一种模式,无论 django 的版本如何:
在您的 models.py 文件中:
from django.db.models.signals import pre_delete
from myapp.signals import pre_delete_signal_dispatcher
pre_delete.connect(pre_delete_signal_dispatcher, sender=MyModel, dispatch_uid="delete_signal_dispatch")
这样,信号在 models.py 文件解析时连接,并在 pre_delete 事件上触发,您可以继续增长您的 signals.py 文件。但是,是的,如果信号连接的数量增加,在应用程序配置中管理它们会更容易。
这是模型文档 pre_delete 上的 运行。当按照建议的最佳实践将此代码放在单独的文件 (signals.py) 中时,它会被皇家忽略。当放入模型文件时,它工作正常。
from django.db.models.signals import pre_delete, pre_save
from django.dispatch import receiver
from _myTools.functions import ImageProcessing
from content_image.models import Document
import os
# Image deletion when deleting entire entry
@receiver(pre_delete, sender=Document, dispatch_uid='document_delete_signal')
def entry_deletion_images_delete(sender, instance, using, **kwargs):
for key, value in instance.imageSizes.items():
name_of_image_field = str(getattr(instance, key)) # Converts to string, if not is object itself
os.remove(instance.baseDir + name_of_image_field)
setattr(instance, key, None)
那么问题是什么?我应该在那里导入更多东西吗?或者我应该将此文件导入某处吗?
问题是,如果您将其放入 signals.py
(按照推荐)但什么都不做,那么就没有人会导入该文件。
您应该遵循 this advice,尤其是
In practice, signal handlers are usually defined in a signals submodule of the application they relate to. Signal receivers are connected in the ready() method of your application configuration class. If you’re using the receiver() decorator, simply import the signals submodule inside ready().
ready()
负责导入模块,触发信号"connection",让信号接收器工作
完整的程序应该是:
# my_app/__init__.py
default_app_config = 'my_app.apps.ConfigForMyApp'
而 apps.py 应该是:
# my_app/apps.py
from django.apps import AppConfig
class ConfigForMyApp(AppConfig):
# Optionally add `name` and `verbose_name`
# for the app
def ready(self): # overriding the ready method
# This will trigger the @receiver decorator
# and thus connect the signals
import my_app.signals
尽管 MariusSiuram 提供的答案是 django >= 1.7 的新推荐方式,但这里是另一种模式,无论 django 的版本如何:
在您的 models.py 文件中:
from django.db.models.signals import pre_delete
from myapp.signals import pre_delete_signal_dispatcher
pre_delete.connect(pre_delete_signal_dispatcher, sender=MyModel, dispatch_uid="delete_signal_dispatch")
这样,信号在 models.py 文件解析时连接,并在 pre_delete 事件上触发,您可以继续增长您的 signals.py 文件。但是,是的,如果信号连接的数量增加,在应用程序配置中管理它们会更容易。