我尝试更新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
方法会生成错误。
答案 0 :(得分:4)
您的to_representation
方法无法返回null值。你忘记了return
陈述。
def to_representation(self, instance):
return super(PhotoThumbnailSerializer,self).to_representation(instance)