Sorl-thumbnail: Use Sorl Thumbnail with Django Rest Framework

Created on 4 Oct 2017  路  7Comments  路  Source: jazzband/sorl-thumbnail

How can I use sorl thumbnail with DRF? I follow instruction Low API: http://sorl-thumbnail.readthedocs.io/en/latest/examples.html#low-level-api-examples and get error: my_file is not defined in model. Please help me.

Most helpful comment

@maxim25
https://github.com/dessibelle/sorl-thumbnail-serializer-field/blob/master/sorl_thumbnail_serializer/fields.py
There is the whole codebase. Nothing about any extra database queries. Please, check your querysets.

All 7 comments

This works:

if obj.photo:
    return get_thumbnail(obj.photo, '30x30', crop='center', quality=99).url

and photo is ImageField in your model.

Thank you. Hope Sorl Thumbnail support for DRF in the future :)

Can you put it in the documentation?

BTW, I've found a useful package for DRF: https://github.com/dessibelle/sorl-thumbnail-serializer-field

@kidig Can you put it at documentation?

@kidig I've found that this library slows down my app significantly. The HyperlinkedSorlImageField generates an additional query to the db and another to the cache (thourgh get_thumbnail). So if you have a simple m2m relationship with ex. 100 images and the image serializer has 2 HyperlinkedSorlImageField (ex one for the thumbnail and one large), it would create an additional 200 queries.

@maxim25
https://github.com/dessibelle/sorl-thumbnail-serializer-field/blob/master/sorl_thumbnail_serializer/fields.py
There is the whole codebase. Nothing about any extra database queries. Please, check your querysets.

Was this page helpful?
0 / 5 - 0 ratings