Django Rest Framework with easy-thumbnails
Asked Answered
W

4

7

With a normal ImageField, serializing the URL is simply image = serializers.ImageField(). What should it look like when using easy-thumbnails?

So far, I've only found a function for getting the URL: Django easy_thumbnails accessing image URLs Using this in a serializer would require a SerializerMethodField, which is an unsatisfying solution. I'm looking for a solution that's as efficient/performant as practical, and on one line.

Welton answered 7/3, 2016 at 1:7 Comment(0)
R
8

Best solution is probably to subclass serializers.ImageField() and use the the code you found in the to_representation method (docs for custom fields). Your field could then look like this:

from easy_thumbnails.templatetags.thumbnail import thumbnail_url

class ThumbnailSerializer(serializers.ImageField):

    def to_representation(self, instance):
        return thumbnail_url(instance, 'small')
Regardless answered 9/3, 2016 at 0:44 Comment(0)
I
3

settings.py

THUMBNAIL_ALIASES = {
    '': {
        'small': {'size': (40, 40)},
        'medium': {'size': (128, 128)},
    },
}

api/serializers.py

from easy_thumbnails.templatetags.thumbnail import thumbnail_url


class ThumbnailSerializer(serializers.ImageField):
    def __init__(self, alias, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.read_only = True
        self.alias = alias

    def to_representation(self, value):
        if not value:
            return None

        url = thumbnail_url(value, self.alias)
        request = self.context.get('request', None)
        if request is not None:
            return request.build_absolute_uri(url)
        return url

using

from api.serializers import ThumbnailSerializer


class ProfileSerializer(serializers.ModelSerializer):
    image = ThumbnailSerializer(alias='medium')
    avatar = ThumbnailSerializer(alias='small', source='image')
Irresolution answered 1/4, 2020 at 4:26 Comment(0)
S
1

What you can do is. Step 1. Install sorl-thumbnail and add it to settings.py

pip install sorl-thumbnail 
./manage.py migrate

Step 2. Install sorl-thumbnail-serializer-field

pip install sorl-thumbnail-serializer-field

Add it to settings.py. Sample usage as stated in documentation (https://github.com/dessibelle/sorl-thumbnail-serializer-field)

class TestModelSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = TestModel

    # A thumbnail image, sorl options and read-only
    thumbnail = HyperlinkedSorlImageField(
        '128x128',
        options={"crop": "center"},
        source='image',
        read_only=True
    )

    # A larger version of the image, allows writing
    image = HyperlinkedSorlImageField('1024')

Django 3.0 compatibility has not been updated yet for sorl-thumbnail. So in case of django 3.0 at the present time.

pip install -e git+git://github.com/jazzband/sorl-thumbnail@4fe1854#egg=sorl-thumbnail 
Schall answered 23/4, 2019 at 8:56 Comment(2)
Please, can you extend your answer with more detailed explanation? Link can expire in any moment, so it is better to have whole description of your solution here. Thank you!Gripping
Generally, links to a tool or library should be accompanied by usage notes, a specific explanation of how the linked resource is applicable to the problem, or some sample code, or if possible all of the above.Flagwaving
C
1

A simple package to serialize easy thumbnailer field: https://github.com/yashas123/easy-thumbnails-rest

Concettaconcettina answered 20/5, 2020 at 6:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.