Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UnicodeDecodeError, which can occur on serializer repr. #2270

Closed
YukSeungChan opened this issue Dec 13, 2014 · 3 comments · Fixed by #2279
Closed

Fix UnicodeDecodeError, which can occur on serializer repr. #2270

YukSeungChan opened this issue Dec 13, 2014 · 3 comments · Fixed by #2279
Labels
Milestone

Comments

@YukSeungChan
Copy link

YukSeungChan commented Dec 13, 2014

Sorry.. I can't speak english well.

Please, see some codes.

class Location(models.Model):
   name = models.CharField(max_length=10)


class LocationSerializer(serializers.ModelSerializer):

    class Meta:
        model = Location

l = Location(name=u'한국')  <<< Unicode
l.save()

s = LocationSerializer(instance=l)
print s

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/loup/.virtualenvs/django-chat/local/lib/python2.7/site-packages/rest_framework/serializers.py", line 399, in __repr__
    return representation.serializer_repr(self, indent=1)
  File "/home/loup/.virtualenvs/django-chat/local/lib/python2.7/site-packages/rest_framework/utils/representation.py", line 77, in serializer_repr
    ret += '\n' + indent_str + field_name + ' = '
UnicodeDecodeError: 'ascii' codec can't decode byte 0xec in position 39: ordinal not in range(128
@YukSeungChan
Copy link
Author

YukSeungChan commented Dec 13, 2014

My patch

utils/representation.py

def serializer_repr(serializer, indent, force_many=None):
    ret = field_repr(serializer, force_many) + ':'
    indent_str = '    ' * indent

    if force_many:
        fields = force_many.fields
    else:
        fields = serializer.fields

    for field_name, field in fields.items():
        field_name = field_name.encode('utf-8')
        ret += '\n' + indent_str + field_name + ' = '
        if hasattr(field, 'fields'):
            ret += serializer_repr(field, indent + 1)
        elif hasattr(field, 'child'):
            ret += list_repr(field, indent + 1)
        elif hasattr(field, 'child_relation'):
            ret += field_repr(field.child_relation, force_many=field.child_relation)
        else:
            ret += field_repr(field)

    if serializer.validators:
        ret += '\n' + indent_str + 'class Meta:'
        ret += '\n' + indent_str + '    validators = ' + smart_repr(serializer.validators)

    return ret

test.py

class Location(models.Model):
   name = models.CharField(max_length=10, unique=True)


class LocationSerializer(serializers.ModelSerializer):

    class Meta:
        model = Location

l = Location(name=u'한국')  <<< Unicode
l.save()

s = LocationSerializer(instance=l)
print s

LocationSerializer(instance=<Location: 한국>):
    id = IntegerField(label='ID', read_only=True)
    name = CharField(max_length=10, validators=[<UniqueValidator(queryset=Location.objects.all())>])

@tomchristie tomchristie added this to the 3.0.2 Release milestone Dec 13, 2014
@tomchristie
Copy link
Member

tomchristie commented Dec 13, 2014

Thanks for the report! :)

@YukSeungChan
Copy link
Author

YukSeungChan commented Dec 13, 2014

@tomchristie Thanks !

@tomchristie tomchristie changed the title serializer_repr func has UnicodeDecodeError Fix UnicodeDecodeError, which can occur on serializer repr. Dec 15, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants