django - 如何将实际图像文件从一个模型复制到另一个模型? [英] django - How to copy actual image file from one model to another?

查看:17
本文介绍了django - 如何将实际图像文件从一个模型复制到另一个模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

现在,我可以做到这些:

获取用户

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

获取用户头像设置

>>>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>

然后:

>>>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>

如何才能在模型中实际复制原始图像file?任何帮助都感激不尽!谢谢.

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

推荐答案

所以我知道这个问题已经很老了,但希望这个答案对某人有所帮助...

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)

请检查在我的情况下,新名称是否与文件名相同,但要在新模型图像字段的路径中更新.在您的情况下,根据您在get_upload_file_name"中的内容,它可能会再次导致相同的路径(因为在两个类中都使用).您也可以创建一个新的随机名称.

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.

希望这对某人有帮助 =)

Hope this helps someone =)

这篇关于django - 如何将实际图像文件从一个模型复制到另一个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆