A
A
apheyhys2018-10-09 23:54:38
Django
apheyhys, 2018-10-09 23:54:38

How to rename uploaded image?

There is a model in which there is a field with which images are saved.
The names of the images are completely arbitrary.
Also in the model we specify the name we need name_cover

class Cover(models.Model):
    name_cover = models.CharField(max_length=100, blank=True, null=True)
    cover = models.ImageField(upload_to='uploads/', blank=True)

How, after saving the model in the admin panel, automatically change the names to the ones we need?
As I understand it, you need to use post_save ?
But what exactly should be written? I saw a lot of code with renaming the save path, but the path just suits me.
def rename_name_cover(.....):

post_save.connect(rename_name_cover, sender=Cover)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Web Dentist, 2018-10-10
@kgb_zor

def content_file_name(instance, filename):
    return '/'.join(['content', instance.user.username, filename])

class Content(models.Model):
    name = models.CharField(max_length=200)
    file = models.ImageField(upload_to=content_file_name)

Something like this

A
apheyhys, 2018-10-10
@apheyhys

You need to change the name of the image, not the path where it will be saved.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question