这就是我将文件从表单上传到后端的FileField:
the_file = request.FILES['newProfilePicture']
the_customer = Customer.objects.first()
the_customer.profile_picture = the_file
the_customer.save()
但问题是,我如何获得该文件字段然后"复制粘贴"它是另一个具有不同的" upload_to"数据?
客户对象:
class Customer( models.Model ):
profile_picture = models.FileField(upload_to='uploads/customer/%Y/%m/%d/')
Customer_Alternative对象:
class Customer_Alternative( models.Model ):
profile_picture = models.FileField(upload_to='uploads/customer-alternative/%Y/%m/%d/')
我目前的问题是,当我执行以下操作时,它只是使用相同的图片而不是" copy-pasting"它到新目录:
old_customer = Customer.objects.all().first()
new_customer_alternative = Customer_Alternative( profile_picture=old_customer.profile_picture, )
new_customer_alternative.save()
因此,如果我删除该Customer对象上的FileField,它也将删除Customer_Alternative的数据。无论如何都要复制粘贴"而不是数据?
我试图做一个深层复制,但失败了?
old_customer = Customer.objects.all().first()
new_customer_alternative = Customer_Alternative( profile_picture=old_customer.profile_picture, )
new_customer_alternative.profile_picture.file = ContentFile(old_customer.profile_picture.read())
new_customer_alternative.save()
答案 0 :(得分:0)
尝试制作文件字段的深层副本,我找到了一个可能有用的链接:Force deep copy of Django FileField object (mainly new file itself)