1
我試圖更新DRF transform_<name>
以使用新的to_representation
方法。當我嘗試這樣做時,我收到以下錯誤,我很難追查。我已在所有串行測試這一點,我得到同樣的事情:to_representation error:'NoneType'對象不可迭代
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
方法中添加會生成錯誤。
我們錯過了大部分回溯。你可以包括它嗎? – 2015-02-06 16:20:04