1

I am using django-imagekit to create thumbnails of my image field.

class CustomUser(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(_('email address'), max_length=254, unique=True)
    image = ProcessedImageField(upload_to= generate_random_filename,
                                processors=[ResizeToFill(640, 640)],
                                format='JPEG',
                                options={'quality': 60})
    avatar = ImageSpecField(source='image',
                            processors=[ResizeToFill(96, 96)],
                            format='JPEG',
                            options={'quality': 60})

Now I created a serializer for the above model using django-rest-framework:

class UserRegistrationSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = tuple(User.REQUIRED_FIELDS) + (
            User.USERNAME_FIELD,
            User._meta.pk.name,
            'image',
            'avatar',
        )

I have created a generics.CreateAPIView to save a new user but, it gives error:

UnicodeDecodeError at /register/
'ascii' codec can't decode byte 0xff in position 11: ordinal not in range(128)

Unicode error hint

The string that could not be encoded/decoded was: "����\u

I don't understand this. Please help. It works fine if I remove avatar from the serializer.

toothie
  • 1,019
  • 4
  • 20
  • 47
  • I don't know django-imagekit, but most probable cause: the serialize tries to put the content of the file instead of its filename, so encoding a bytes-object while the serializers expect a human readable string (= str object) – Maxime Lorant Oct 19 '16 at 13:13
  • @MaximeLorant Is there a solution to this? I want the url of the 'avatar' image field to be returned by the requests. – toothie Oct 19 '16 at 13:19
  • @toothie try these http://stackoverflow.com/questions/21129020/how-to-fix-unicodedecodeerror-ascii-codec-cant-decode-byte or http://stackoverflow.com/questions/10561923/unicodedecodeerror-ascii-codec-cant-decode-byte-0xef-in-position-1 – anjaneyulubatta505 Oct 19 '16 at 14:11

0 Answers0