I would like to change the name of an image coming renaming it to the color of the door.
Here is my code:
class Door(models.Model) :
image = models.ImageField(upload_to='doors')
color = models.ForeignKey(Color, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10, decimal_places=2, default='119.99')
I've looked at multiple things but I don't know yet how to do it.
Please help me if you know the answer to my problem.
You can do like this:
def upload_location(instance, filename):
filebase, extension = filename.split('.')
return 'images/%s.%s' % (instance.color.name, extension)
class Color(models.Model):
name = models.CharField(max_length=20)
class Door(models.Model) :
image = models.ImageField(upload_to=upload_location)
color = models.ForeignKey(Color, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10, decimal_places=2, default='119.99')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With