0

Does anyone know how to store a clone of the original image to another model on Django.

This is my models.py file

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"

I am looking to clone the image field in this model and store to clone image model

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
WalterW
  • 47
  • 4

1 Answers1

2

You Do image field as clone with cone_image field with overriding save() method like this...

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)

Output

enter image description here