首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >to_representation错误:“NoneType”对象不可迭代

to_representation错误:“NoneType”对象不可迭代
EN

Stack Overflow用户
提问于 2015-02-06 16:12:47
回答 1查看 1.3K关注 0票数 0

我正在尝试更新DRF transform_<name>以使用新的to_representation方法。当我尝试这样做的时候,我得到了下面的错误,我发现很难追踪。我已经在我的所有序列化器上测试了这一点,并且得到了相同的结果:

代码语言:javascript
复制
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

我的代码:

代码语言:javascript
复制
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方法将生成错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-06 16:33:46

to_representation方法无法返回空值。你忘了return声明。

代码语言:javascript
复制
def to_representation(self, instance):
    return super(PhotoThumbnailSerializer,self).to_representation(instance) 
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28370037

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档