相关疑难解决方法(0)

Django:保存时,如何检查字段是否已更改?

在我的模型中,我有:

class Alias(MyBaseModel):
    remote_image = models.URLField(max_length=500, null=True, help_text="A URL that is downloaded and cached for the image. Only
 used when the alias is made")
    image = models.ImageField(upload_to='alias', default='alias-default.png', help_text="An image representing the alias")


    def save(self, *args, **kw):
        if (not self.image or self.image.name == 'alias-default.png') and self.remote_image :
            try :
                data = utils.fetch(self.remote_image)
                image = StringIO.StringIO(data)
                image = Image.open(image)
                buf = StringIO.StringIO()
                image.save(buf, format='PNG')
                self.image.save(hashlib.md5(self.string_id).hexdigest() + ".png", ContentFile(buf.getvalue()))
            except IOError :
                pass
Run Code Online (Sandbox Code Playgroud)

这首次remote_image变化很有效.

当有人修改remote_image了别名时,如何获取新图像?其次,是否有更好的方法来缓存远程图像?

django caching image django-models

273
推荐指数
13
解决办法
14万
查看次数

标签 统计

caching ×1

django ×1

django-models ×1

image ×1