我使用sorl_thumbnail在tastype中生成缩略图,如:
class ImageResource(ModelResource):
class Meta:
always_return_data=True
filtering = {
"album": ('exact',),
}
queryset = Image.objects.all()
cache = SimpleCache(timeout=100)
resource_name = 'image'
authorization = ImageAuthorization()
def dehydrate(self, bundle):
im = get_thumbnail(bundle.obj.src, '200x200', quality=90)
bundle.data['thumb'] = im.url
return bundle
现在我有一个专辑资源,我想把最后上传的图片作为专辑的封面图片,这是我目前为止的图片:
class AlbumResource(ModelResource):
cover_img = fields.ForeignKey('album.api.ImageResource')
这只会给我封面图片的url,比如
'album/v1/image/99'
我想要的是当我获得资源时,我可以显示封面图像,比如
{
cover_img:path_to_cover_thumb,
}
我该怎么做呢?
好吧,我终于得到它的工作,这里是我所做的:
class AlbumResource(ModelResource):
cover_img = fields.ForeignKey('album.api.ImageResource','cover_img')
class Meta:
always_return_data=True
queryset = Album.objects.all()
resource_name = 'album'
authorization = AlbumAuthorization()
def dehydrate(self, bundle):
im = get_thumbnail(bundle.obj.cover_img.src, '200x200', quality=90)
bundle.data['cover_thumb'] = im.url
return bundle