在Django的管理中上传文件时,我试图使用信号创建和重命名该文件的副本。但是,我收到以下错误:
'FileDescriptor‘对象没有属性'path’
模型
class Template(models.Model):
organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
def org_folder(self, filename):
path = "templates/%s/%s/%s" % (self.organization.name, self.name, filename)
return path
docxfile = models.FileField(upload_to=org_folder)信号
@receiver(post_save, sender=Template)
def create_clean_docxfile(sender, instance, *args, **kwargs):
if sender is Template:
file = Template.docxfile.path
copyfile(file, 'temp.zip')有什么建议吗?
更新
根据丹尼尔的建议,我清除了错误。我必须将信号更新如下:
@receiver(post_save, sender=Template)
def create_clean_docxfile(sender, instance, *args, **kwargs):
file = instance.docxfile.path
copyfile(file, 'temp.zip')发布于 2019-03-17 18:08:29
发送者是类。您需要访问实例,该实例由实例参数提供。(你不需要那个if语句。)
def create_clean_docxfile(sender, instance, *args, **kwargs):
file = instance.docxfile.path
copyfile(file, 'temp.zip')https://stackoverflow.com/questions/55210251
复制相似问题