【问题标题】:how to reference to a thumb attribute in tastypie resource foreign key?如何引用tastepie资源外键中的拇指属性?
【发布时间】:2023-04-09 12:33:01
【问题描述】:

我使用 sorl_thumbnail 来生成tastepie 中的缩略图,例如:

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,
 }

我该怎么做?

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多