matthewwithanm / django-imagekit

Automated image processing for Django. Currently v4.0
http://django-imagekit.rtfd.org/
BSD 3-Clause "New" or "Revised" License
2.26k stars 276 forks source link

thumbnail does not work with Django REST #457

Closed elcolie closed 6 years ago

elcolie commented 6 years ago

Here is my repository https://github.com/elcolie/django-image-discussion

Problems:

References: https://github.com/matthewwithanm/django-imagekit/issues/456

vstoykov commented 6 years ago

For admin page to work you need to use admin_thumbnail in list_display instead of thumbnail. Currently you are only defining the attribute admin_thumbnail but does not list it in list_display for that reason you are not seeing the preview but instead see the relative path to the storage where cached thumbnail is stored.

About the issue with the REST Framework you can look at the code for ModelSerializer https://github.com/encode/django-rest-framework/blob/3.7.7/rest_framework/serializers.py#L839-L864 there is a mapping between default Django fields and REST Framework fields.

When you are using default models.ImageField then REST Framework know how to serialize it. The problem is that it does not know anything about 3th party custom fields. You can help it by explicitly say which is the type of the field:

class ExampleSerializer(serializers.ModelSerializer):
    thumbnail = serializers.ImageField()

    class Meta:
        model = Example
        fields = [
            'image',
            'thumbnail',
        ]

I didn't tested it but you can try.

elcolie commented 6 years ago

Thank you very much. I will commit your answer to my repo. It is good example.