1

Model available:

photo = models.ImageField(verbose_name='Фото', upload_to='images/human/%Y/%m/%d/', 
default='default/user.png', blank=True)
    photo_200 = ImageSpecField(
        source='photo',
        processors=[ResizeToFill(200, 200)],
        format='JPEG',
        options={'quality': 80},
    )
    photo_272 = ImageSpecField(
        source='photo',
        processors=[ResizeToFill(272, 250)],
        format='JPEG',
        options={'quality': 80},
    )

serializer:

class HumanListSerializer(serializers.ModelSerializer):

    class Meta:
        model = Human
        fields = (
            'name',
            'surname',
            'middle_name',
            'description',
            'photo_272',
            'slug'
        )

I get an error on photo_272 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

with picture photo everything is in order, since it is not used django rest framework

2
  • 1) Speculative guess: you are using wrong encoding, fixing the encoding should solve the problem. At least for me it was the case. 2) You are not showing the part of your code where the decoding takes place, this is the part where the error occurs. Commented Apr 19, 2021 at 18:06
  • Please add more info about the error, like, the complete error traceback, method to reproduce the error etc Commented Apr 20, 2021 at 8:56

1 Answer 1

5
+50

I think the django rest framework doesn't know how to serialize ImageSpecField properly since it's not a standard django model field. So you need to add a custom method to the serializer

class HumanListSerializer(serializers.ModelSerializer):
    photo_272 = serializers.SerializerMethodField()
    class Meta:
        model = Human
        fields = (
            'name',
            'surname',
            'middle_name',
            'description',
            'photo_272',
            'slug'
        )
    
    def get_photo_272(self, record: Human):
        return record.photo_272.url
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.