Django Rest Framework with easy-thumbnails

家住魔仙堡 提交于 2021-01-26 14:47:10

问题


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.


回答1:


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



回答2:


settings.py

THUMBNAIL_ALIASES = {
    '': {
        'avatar': {'size': (40, 40)},
        'image': {'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='image')
    avatar = ThumbnailSerializer(alias='avatar', source='image')



回答3:


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 



回答4:


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



来源:https://stackoverflow.com/questions/35834664/django-rest-framework-with-easy-thumbnails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!