cloudinary cloudinary.models.CloudinaryField将public_id添加到字段中

时间:2012-10-18 18:29:25

标签: python django cloudinary

这是一个简单的模型:

class Product(models.Model):

caption = models.CharField(max_length=1000)
description=models.TextField()
supplier = models.ForeignKey(Supplier)
designNumber = models.CharField(max_length=100)
price = models.IntegerField()
stock = models.IntegerField()
parameters = models.ManyToManyField(ProductParameterValues)
image = cloudinary.models.CloudinaryField('image')
added_on = models.DateTimeField(auto_now=True)
def __unicode__(self):
    return self.designNumber + ":" + self.caption + ":" + self.supplier.name

我希望能够让图像(cloudinary图像)具有由其他参数组成的公共ID - 例如supplier.user.id和designNumber。

我们如何做到这一点?

感谢。

1 个答案:

答案 0 :(得分:3)

在最新版本的Python库中,您可以从CloudinaryField继承,并从模型的属性中确定public_id(和其他上传参数),例如:

class MyCloudinaryField(CloudinaryField):
    def upload_options(self, model_instance):
       return {'public_id': model_instance.supplier.user.id + "-" + model_instance.designNumber}