django 信号,如何使用“实例”

发布于 2024-11-28 23:54:38 字数 660 浏览 0 评论 0原文

我正在尝试创建一个系统,使用户能够上传 zip 文件,然后使用 post_save 信号提取它。

class Project:
    ....
    file_zip=FileField(upload_to='projects/%Y/%m/%d')

@receiver(post_save, sender=Project)
def unzip_and_process(sender, **kwargs):
    #project_zip = FieldFile.open(file_zip, mode='rb')
    file_path = sender.instance.file_zip.path
    with zipfile.ZipFile(file_path, 'r') as project_zip:
        project_zip.extractall(re.search('[^\s]+(?=\.zip)', file_path).group(0))
        project_zip.close()

当提供正确的文件路径时,unzip_and_process 方法可以正常工作(在这种情况下,我需要提供instance.file_zip.path。但是,我无法获取/设置实例关于信号的 Django 文档不清楚并且没有示例,那么我该怎么办?

I am trying to create a system which enables user to upload a zipfile, and then extract it using post_save signal.

class Project:
    ....
    file_zip=FileField(upload_to='projects/%Y/%m/%d')

@receiver(post_save, sender=Project)
def unzip_and_process(sender, **kwargs):
    #project_zip = FieldFile.open(file_zip, mode='rb')
    file_path = sender.instance.file_zip.path
    with zipfile.ZipFile(file_path, 'r') as project_zip:
        project_zip.extractall(re.search('[^\s]+(?=\.zip)', file_path).group(0))
        project_zip.close()

unzip_and_process method works fine when correct file paths are provided(in this case, i need to provide instance.file_zip.path. However, I couldn't get/set the instance with the signals. Django documentation about signals is not clear and have no examples. So, what do I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时间海 2024-12-05 23:54:38

实际上,Django 关于信号的文档非常清楚并且包含示例。

在您的情况下, post_save 信号发送以下参数:sender(模型类)、instancesender 类的实例)、created、<代码>原始和使用。如果您需要访问实例,可以在示例中使用 kwargs['instance'] 来访问它,或者更好的是,更改回调函数以接受参数:

@receiver(post_save, sender=Project)
def unzip_and_process(sender, instance, created, raw, using, **kwargs):
    # Now *instance* is the instance you want
    # ...

Actually, Django's documentation about signals is very clear and does contain examples.

In your case, the post_save signals sends the following arguments: sender (the model class), instance (the instance of class sender), created, raw, and using. If you need to access instance, you can access it using kwargs['instance'] in your example or, better, change your callback function to accept the argument:

@receiver(post_save, sender=Project)
def unzip_and_process(sender, instance, created, raw, using, **kwargs):
    # Now *instance* is the instance you want
    # ...
如日中天 2024-12-05 23:54:38

这在连接Django Signals时对我有用:

这是models.py

class MyModel(models.Model):
    name = models.CharField(max_length=100)

以及访问它的Signalpost_save >:

@receiver(post_save, sender=MyModel)
def print_name(sender, instance, **kwargs):
    print '%s' % instance.name 

This worked for me when connecting Django Signals:

Here is the models.py:

class MyModel(models.Model):
    name = models.CharField(max_length=100)

And the Signal that access it post_save:

@receiver(post_save, sender=MyModel)
def print_name(sender, instance, **kwargs):
    print '%s' % instance.name 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文