Upload photo with model name in Django

Asked

Viewed 895 times

0

I would like help for a task to save images with a custom name and a folder also with the current name, suppose the following Model:

class ProdutoA(models.Model):
    nome_produto_a = models.CharField(max_length=50, verbose_name='Nome do Produto')
    foto_produto_a = models.ImageField(upload_to='produtos', blank=True, null=True, verbose_name='Foto do Produto')
    # ...

class ProdutoB(models.Model):
    nome_produto_b = models.CharField(max_length=50, verbose_name='Nome do Produto')
    foto_produto_b = models.ImageField(upload_to='produtos', blank=True, null=True, verbose_name='Foto do Produto')
    # ...

My doubt:

To save the image with the name of the class "Produtox" + "nome_producto_x", that is, name of the model/class that is registered + name of the product?

For example, suppose there is a product registered in Productoa:

product name_a = Rice 101

photo_product = any.jpg

When saving this image, I would like to rename to:

Produtoa.Arroz101.jpg

Inside a folder called "Product A".

It therefore results in:

Product A/Productoa.Arroz101.jpg

1 answer

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.