1
每当我创建Cloth
型号与cloth_image
,图像上传到upload_location
。可以为ImageField上传位置的文件名是什么?
我的问题是instance.id
返回None
,因为该实例尚未创建。 什么是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)
我正在使用AWS S3。
我喜欢这个主意。如果uuid.uuid4()生成以前的ID呢?他们是否将以前生成的ID存储到某个地方?如果不是,它只是随机整数,不是吗? –
这是基于许多参数的综合字段,你可以阅读更多[uuid](https://docs.python.org/2/library/uuid.html) –
嗯..确切地说。我想为图像文件名生成REAL唯一ID。 :( –