to_representation error: 'NoneType' object is not iterable

to_representation error: 'NoneType' object is not iterable

我正在尝试更新 DRF transform_<name> 以使用新的 to_representation 方法。当我尝试这样做时,我遇到了以下错误,我发现很难找到它。我已经在我所有的序列化器上测试过了,我得到了同样的结果:

Traceback (most recent call last):
  File "/Users/glyn/Documents/workspace/app/django-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/glyn/Documents/workspace/app/django-env/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/glyn/Documents/workspace/app/django-env/lib/python2.7/site-packages/django/views/generic/base.py", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/glyn/Documents/workspace/app/django-env/lib/python2.7/site-packages/rest_framework/views.py", line 407, in dispatch
    response = self.handle_exception(exc)
  File "/Users/glyn/Documents/workspace/app/django-env/lib/python2.7/site-packages/rest_framework/views.py", line 404, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/glyn/Documents/workspace/app/app/apps/ornamentation/views/photo.py", line 23, in get
    return self.retrieve(request, *args, **kwargs)
  File "/Users/glyn/Documents/workspace/app/django-env/lib/python2.7/site-packages/rest_framework/mixins.py", line 56, in retrieve
    return Response(serializer.data)
  File "/Users/glyn/Documents/workspace/app/django-env/lib/python2.7/site-packages/rest_framework/serializers.py", line 464, in data
    return ReturnDict(ret, serializer=self)
  File "/Users/glyn/Documents/workspace/app/django-env/lib/python2.7/site-packages/rest_framework/utils/serializer_helpers.py", line 14, in __init__
    super(ReturnDict, self).__init__(*args, **kwargs)
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/collections.py", line 52, in __init__
    self.__update(*args, **kwds)
  File "/Users/glyn/Documents/workspace/app/django-env/bin/../lib/python2.7/_abcoll.py", line 566, in update
    for key, value in other:
TypeError: 'NoneType' object is not iterable

我的代码:

class ThumbnailSerializerMixin(serializers.HyperlinkedModelSerializer):
    """
    Mixin used to create a thumbnail based on parameters.
    If no parameters have been passed defaults are used.
    """

    thumbnail_image = HyperlinkedImageField()
    thumbnail = HyperlinkedImageField()


    def to_representation(self, instance):
            super(PhotoThumbnailSerializer,self).to_representation(instance)

    def transform_thumbnail(self, obj, value):
        """
        :param: thumbnail_width, thumbnail_height, thumbnail_quality,
        :return: S3 signed URL to thumbnail.
        """

        if not value == "null":
            width = self.context['request'].GET.get('thumbnail_width', settings.THUMBNAIL_DEFAULT_WIDTH)
            height = self.context['request'].GET.get('thumbnail_height', settings.THUMBNAIL_DEFAULT_HEIGHT)
            quality = self.context['request'].GET.get('thumbnail_quality', settings.THUMBNAIL_DEFAULT_QUALITY)
            return urllib.quote(obj.thumbnail(width=width, height=height, quality=quality).url, safe="%/:=&?~#+!$,;'@()*[]")
        return "null"

    class Meta:
        abstract = True
        fields = ("url", "thumbnail_image", "thumbnail",)


    class PhotoThumbnailSerializer(ThumbnailSerializerMixin):
        url = serializers.HyperlinkedIdentityField(view_name='photo_detail')
        raw_image = HyperlinkedImageField()

        class Meta(ThumbnailSerializerMixin.Meta):
            model = Photo
            fields = ("url", "raw_image", "thumbnail",)

注意:上面我留在 transform_thumbnail 中,这是我的旧方法。添加 to_representation 方法会产生错误。

您的 to_representation 方法不能 return 空值。你忘记了 return 语句。

def to_representation(self, instance):
    return super(PhotoThumbnailSerializer,self).to_representation(instance)