如何在tastypie资源外键中引用thumb属性?

时间:2013-05-27 09:21:45

标签: django resources thumbnails foreign-key-relationship tastypie

我使用sorl_thumbnail在tastypie中生成缩略图,如:

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')

这只会给我封面图片的网址

'album/v1/image/99'

我想要的是当我获得资源时,我可以实际显示封面图像,如

 {
    cover_img:path_to_cover_thumb,
 }

我该怎么做?

1 个答案:

答案 0 :(得分:1)

好吧,我终于得到它的作品就是我所做的:

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