【问题标题】:cloudinary cloudinary.models.CloudinaryField add public_id to the fieldcloudinary cloudinary.models.CloudinaryField 将 public_id 添加到该字段
【发布时间】:2012-10-09 07:19:41
【问题描述】:

这是一个简单的模型:

类产品(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

我希望能够让图像(云图像)有一个由其他参数组成的公共 id - 例如供应商.user.id 和 designNumber。

我们如何做到这一点?

谢谢。

【问题讨论】:

    标签: python django cloudinary


    【解决方案1】:

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

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

    【讨论】:

    • 在编辑操作期间,我看不到访问字段现有 public_id 的方法。我查看了selfmodel_instance 上可用的内容。我错过了吗?如果它已经有一个,我想确保它保留其现有的 public_id。
    猜你喜欢
    • 2017-04-20
    • 2015-10-20
    • 2015-12-06
    • 2023-03-23
    • 2021-06-19
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    相关资源
    最近更新 更多