我怎样把镜像的克隆保存到Django的另一个模型中

5rgfhyps  于 2022-12-14  发布在  Go
关注(0)|答案(1)|浏览(137)

有人知道如何将原始图像的克隆存储到Django上的另一个模型中吗?
这是我的models.py文件

class blogs(models.Model)
    title = models.CharField(max_length=100, null=True, blank=True)
    slug = models.SlugField(max_length=200, unique=True, auto_created=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    created_on = models.DateTimeField(auto_now_add=True)
    time = models.TimeField(auto_now_add=True)
    cone_image = models.ImageField(upload_to='cloned')
    image = models.ImageField(upload_to='photos',height_field="image_height", width_field="image_width"

我希望克隆此模型中的图像字段并存储到克隆图像模型

mrfwxfqh

mrfwxfqh1#

您将image字段作为cone_image字段的克隆,并覆盖保存()方法,如下所示...

models.py

class blogs(models.Model)
    title = models.CharField(max_length=100, null=True, blank=True)
    slug = models.SlugField(max_length=200, unique=True, auto_created=True)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    created_on = models.DateTimeField(auto_now_add=True)
    time = models.TimeField(auto_now_add=True)
    clone_img = models.ImageField(upload_to="images",blank=True,null=True)
    image = models.ImageField(upload_to='photos',height_field="image_height", width_field="image_width")

    def save(self, *args, **kwargs):
        self.clone_img = self.img
        super(blogs, self).save(*args, **kwargs)

输出

相关问题