- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在删除 File
实例时从存储中删除文件。尝试使用 django 信号。这是我的模型文件:
class File(models.Model):
orig_name = models.CharField(_('Original Name'), max_length=255)
conversation = models.ForeignKey('conversation.Conversation', on_delete=models.CASCADE)
每个应用程序的架构都是相同的。意味着所有应用程序都有自己的 signals
目录,在 signals 目录中我们有 __init__.py
和 handlers.py
文件。这里是我的应用程序的简短树,其中包含 file
的应用程序的一些细节:
apps
├── conversation
├── post
└── file
├── admin.py
├── apps.py
├── __init__.py
├── models.py
├── signals
│ ├── handlers.py
│ └── __init__.py
├── tests.py
├── urls.py
└── views.py
这里是apps/file/apps.py
:
from django.apps import AppConfig
class FileConfig(AppConfig):
name = 'apps.file' # exact as INSTALLED_APP in setting.py
def ready(self):
import apps.file.signals.handers
这里是 apps/file/signals/handlers.py
:
from django.db.models.signals import pre_delete
from django.dispatch import receiver
from apps.file.models import File
@receiver(pre_delete, sender=File)
def remove_file_from_storage(sender, instance, *args, **kwargs):
print('pre_delete signal for File working')
# some code is here
和app/file/signals/__init__.py
文件是空的。在这个项目中,我们使用了多个信号,它们都工作正常,但我不知道为什么这个信号不起作用。其他信号来自自定义信号和内置信号。请注意,当我将 def remove_file_from_storage
函数移动到其他应用程序到相关的 app_name/signals/handlers.py
时,它工作正常。删除过程是一个简单的 泛型。销毁 DRF 的 APIView
。我的架构师使用信号的问题在哪里?谢谢
最佳答案
问题是因为你使用了 receiver
装饰器来注册信号。因此,您必须将代码放入 Django 应用程序的现成函数中。 This document确实提到了这一点:
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
方法中,这可能会使我们的代码更难阅读和更难维护。因此,就像您所做的那样,我们会将信号放在 signals.py
中,然后通过 ready
方法将其加载到您的应用中,如下所示:
from django.apps import AppConfig
class FileConfig(AppConfig):
label = 'file'
name = 'file'
def ready(self):
from apps.file import signals # NOQA
# add # NOQA to ignore error message from Python linter when we just import but never used
并将其设置为 __init__.py
以便 Django 可以发现您的应用:
apps
├── conversation
├── post
└── file
├── admin.py
├── apps.py
├── __init__.py # Add to this file
├── models.py
├── signals
│ ├── handlers.py
│ └── __init__.py
├── tests.py
├── urls.py
└── views.py
default_app_config = 'apps.file.apps.FileConfig'
你也可以看看 this article获取有关 Django 信号的更多详细信息
希望对您有所帮助!
关于django - pre_delete 信号在特定目录中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58152967/
我想在删除 File 实例时从存储中删除文件。尝试使用 django 信号。这是我的模型文件: class File(models.Model): orig_name = models.Cha
我有一个模型“评论”和一个在评论被删除时采取行动的信号。该信号在管理中删除评论时执行,但通过 django-rest-framework 删除时不执行。 @receiver(post_delete,
这是在模型文档的 pre_delete 上运行的。当按照建议的最佳实践将此代码放在单独的文件 (signals.py) 中时,它会被皇家忽略。当放入模型文件时,它工作正常。 from django.d
是否可以在连接到 pre_delete 信号的回调中获取登录的 django 用户(调用模型的删除方法)? 最佳答案 pre_delete 信号不会传递请求实例,但您可以添加一个装饰器来添加它,并在
我正在使用信号在对象被删除后执行操作;但是,有时我想根据参数执行不同 操作(不是默认操作)。 有没有办法将参数传递给我的信号捕捉器?或者我是否必须放弃信号,而是硬编码我想在模型中执行的操作? 我想做的
在我的应用程序中,我想跟踪所有被删除的问题。所以我在我的模型文件中创建了一个类(表)。 class Deleted(models.Model): question = models.IntegerFi
我是一名优秀的程序员,十分优秀!