在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')
有什么建议吗?
更新
根据Daniel的建议,我清除了错误。我不得不将信号更新为以下内容:
@receiver(post_save, sender=Template)
def create_clean_docxfile(sender, instance, *args, **kwargs):
file = instance.docxfile.path
copyfile(file, 'temp.zip')
答案 0 :(得分:0)
发件人是课程。您需要访问由instance参数提供的实例。 (而且您不需要该if语句。)
def create_clean_docxfile(sender, instance, *args, **kwargs):
file = instance.docxfile.path
copyfile(file, 'temp.zip')