Whenever I create Cloth
Model with cloth_image
, the image uploads to upload_location
.
My problem is instance.id
returns None
because the instance is not yet created. What can be the best upload_location for ImageField?
def upload_location(instance, filename):
new_id = instance.id
ext = filename.split('.')[-1]
return "clothes/%s/%s.%s" % (instance.user.id, new_id, ext) # <- Right Here!
class Cloth(models.Model):
cloth_image = models.ImageField(upload_to=upload_location,
null=True,
blank=True)
I'm using AWS S3.