K
K
krasdev2019-08-18 15:53:44
Django
krasdev, 2019-08-18 15:53:44

Django how to rename already loaded ImageField images?

Changed the Model field with ImageField image upload field

image = models.ImageField(upload_to=motor_image_rename, default=None, blank=True, unique=True)

added a function to rename them and place them in a specific folder
def motor_image_rename(instance, filename):
    ext = filename.split('.')[-1]
    name = slugify(instance.motor.brand.title),slugify(instance.motor.title)
    filename = "%s.%s" % (name, ext)
    return os.path.join('motor/{brand}/'.format(brand=instance.motor.brand.title), filename)

The question is how to apply it now to those pictures that have already been uploaded? So that they are renamed and moved to the desired folder

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question