django—如何将实际的图像文件从一个模型复制到另一个模型?

时间:2022-04-11 20:27:51

I want to copy images from one model to another within the project. Suppose these are my models:

我想在项目中把图像从一个模型复制到另一个模型。假设这些是我的模型:

class BackgroundImage(models.Model):
    user = models.ForeignKey(User)
    image = models.ImageField(upload_to=get_upload_file_name)
    caption = models.CharField(max_length=200)
    pub_date = models.DateTimeField(default=datetime.now)


class ProfilePicture(models.Model):
    user = models.ForeignKey(User)
    image = models.ImageField(upload_to=get_upload_file_name)
    caption = models.CharField(max_length=200)
    pub_date = models.DateTimeField(default=datetime.now)

    @classmethod
        def create_from_bg(cls, bg_img):
            img = cls(user=bg_img.user, image=bg_img.image, caption=bg_img.caption+'_copy', pub_date=bg_img.pub_date)
            img.save()
            return img

For now, I can do these:

现在,我可以这样做:

To get the user

让用户

>>>m = User.objects.get(username='m')

To get the user's profile picture set

获取用户的配置文件图片集

>>>m_pro_set = m.profilepicture_set.all()
>>>m_pro_set
[<ProfilePicture: pro_mik>]

Get an image object from Background image of the user

从用户的背景图像中获取图像对象

>>>m_back_1 = m.backgroundimage_set.get(id=2)
>>>m_back_1
<BackgroundImage: bg_mik>

And then:

然后:

>>>profile_pic = ProfilePicture.create_from_bg(m_back_1)

Now when I check it, it does create a new instance.

现在当我检查它时,它确实创建了一个新的实例。

>>>m_pro_set
[<ProfilePicture: pro_mik>,<ProfilePicture: bg_mik>]

But, if I check on the path, and even on the media folder, its the same image and not an actual copy of the image file.

但是,如果我检查路径,甚至是媒体文件夹,它是相同的图像,而不是图像文件的实际拷贝。

>>>profile_pic.image
<ImageFileField: uploaded_files/1389904144_ken.jpg>
>>>m_back_1.image
<ImageFileField: uploaded_files/1389904144_ken.jpg>

How do I go about, to actually copy the original image file within the models? Any help will be much appreciated! Thank you.

如何在模型中复制原始图像文件?如有任何帮助,我们将不胜感激!谢谢你!

1 个解决方案

#1


2  

so I know this question is pretty old, but hopefully this answer help someone...

我知道这个问题由来已久,但希望这个答案能帮助某人……

My approach for doing this, uploading the photo to the proper path for the suggested model, is:

我这样做的方法是,将照片上传到所建议的模型的合适路径,即:

from django.core.files.base import ContentFile

picture_copy = ContentFile(original_instance.image.read())
new_picture_name = original_instance.image.name.split("/")[-1]
new_instance.image.save(new_picture_name, picture_copy)

Please check that in my case, the new name is just the same file name, but to be updated in the new model image field's path. In your case, depending on what you have inside "get_upload_file_name" it could leads to the same path again (since is used in both classes). Also you can create a new, random name.

请检查一下,在我的例子中,新名称只是相同的文件名,但是要在新模型图像字段的路径中更新。在您的例子中,根据您在“get_upload_file_name”中的内容,它可能再次导致相同的路径(因为在两个类中都使用)。您还可以创建一个新的、随机的名称。

Hope this helps someone =)

希望这对某人有帮助

#1


2  

so I know this question is pretty old, but hopefully this answer help someone...

我知道这个问题由来已久,但希望这个答案能帮助某人……

My approach for doing this, uploading the photo to the proper path for the suggested model, is:

我这样做的方法是,将照片上传到所建议的模型的合适路径,即:

from django.core.files.base import ContentFile

picture_copy = ContentFile(original_instance.image.read())
new_picture_name = original_instance.image.name.split("/")[-1]
new_instance.image.save(new_picture_name, picture_copy)

Please check that in my case, the new name is just the same file name, but to be updated in the new model image field's path. In your case, depending on what you have inside "get_upload_file_name" it could leads to the same path again (since is used in both classes). Also you can create a new, random name.

请检查一下,在我的例子中,新名称只是相同的文件名,但是要在新模型图像字段的路径中更新。在您的例子中,根据您在“get_upload_file_name”中的内容,它可能再次导致相同的路径(因为在两个类中都使用)。您还可以创建一个新的、随机的名称。

Hope this helps someone =)

希望这对某人有帮助