【问题标题】:How to copy an object from one model to another model如何将对象从一个模型复制到另一个模型
【发布时间】:2013-11-05 19:33:17
【问题描述】:

每个用户都有很多 相册,每个相册都有很多 照片。每个用户都有一组 背景图片来保存它的许多图片。同样,用户有一组个人资料图片来保存其许多图像。

这些是我的模型:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    permanent_address = models.TextField()
    temporary_address = models.TextField()
    profile_pic = models.ForeignKey(ProfilePicture)
    background_pic = models.ForeignKey(BackgroundImage)


class Album(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=200)
    pub_date = models.DateTimeField(default=datetime.now)


class Photo(models.Model):
    album = models.ForeignKey(Album, default=3)
    image = models.ImageField(upload_to=get_upload_file_name)
    caption = models.CharField(max_length=200)
    pub_date = models.DateTimeField(default=datetime.now)


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)

例如:我尝试这样做:让用户将 一个 图像ProfilePicture 复制到 Background Image ,但没有用:

# Get the user
>>>m = User.objects.get(username='m')

# Get its set of Profile pictures
>>>m_pro_set = m.profilepicture_set.all()
>>>m_pro_set
[<ProfilePicture: pro_mik>]

# Get its set of Background images
>>>m_back_set = m.backgroundimage_set.all()
>>>m_back_set
[<BackgroundImage: bg_mik>]

# Get an image object from Profile picture of the user
>>>m_pro_1 = m.profilepicture_set.get(id=2)
>>>m_pro_1
<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>

# So, I tried to copy one image from BackgroundImage of a user to ProfilePicture

>>>m_pro_set.objects.create(m_back_1)

    File "<console>", line 1, object has attribute 'objects'
AttributeError: 'QuerySet' object has no attribute 'objects'

所以我的问题是,如何将对象从一个模型复制到另一个模型?任何建议将不胜感激。

【问题讨论】:

    标签: django django-models django-orm


    【解决方案1】:

    您需要将一个对象的属性复制到另一个对象。 create 在您想要创建新实例时使用,而不是在您想要更新特定实例时使用。

    m_pro = m_pro_set[0] # m_pro_set is a list and need to get the specific instance
    m_pro.image = m_back_1.image
    m_pro.caption = m_back_1.caption
    m_pro.pub_date = m_back_1.pub_date
    m_pro.save()
    

    如果这是包含此功能的常用操作,您也可以在 ProfilePicture 上创建一个方法。

    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)
    
        def update(self, bg_img):
            self.image = bg_img.image
            self.caption = bg_img.caption
            self.pub_date = bg_img.pub_date
            self.save()
    

    因此,如果我们想让m_pro_1 具有与m_back_1 相同的属性,您只需调用m_pro_1.update(m_back_1)

    另外,如果你想创建一个新实例,我建议使用classmethod,如下所示

    class ProfilePicture(models.Model):
        ...
    
        @classmethod
        def create_from_bg(cls, bg_img):
            img = cls(user=bg_img.user, image=bg_img.image, caption=bg_img.caption, pub_date=bg_img.pub_date)
            img.save()
            return img
    
        @classmethod
        def create_from_photo(cls, photo):
            img = cls(user=photo.album.user, image=photo.image, caption=photo.caption, pub_date=photo.pub_date)
            img.save()
            return img
    

    这会创建一个新的ProfilePicture 并使用:profile_pic = ProfilePicture.create_from_bg(m_back_1)

    【讨论】:

    • 感谢您的回答!我尝试了这里的方式。但我猜你误会了。我想要的是从一个模型复制图像,然后在另一个模型中创建它,但保留原始图像。希望我说清楚了。
    • 使用类方法方法。这是一个生成新对象的工厂函数。您可以为每种类型创建一个(参考您对另一个答案的评论)。
    • 哦!对不起。我没仔细看。我认为它更容易。但是我如何创建一个从模型 Photo 到 ProfilePic 或 BackgroundImage 的新实例。流程一样吗?
    • 我在答案中添加了一些内容,向您展示了它的外观。基本上,您可以使用任何参数作为参数,只要您可以从中定义模型的属性。
    • 这是否回答了您的问题,或者您是否需要进一步澄清?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多