我正在为我的Django-Rest-Framework API使用此序列化程序类。 (来自:https://github.com/dessibelle/sorl-thumbnail-serializer-field)
class HyperlinkedSorlImageField(serializers.ImageField):
def __init__(self, geometry_string, options={}, *args, **kwargs):
"""
Create an instance of the HyperlinkedSorlImageField image serializer.
Args:
geometry_string (str): The size of your cropped image.
options (Optional[dict]): A dict of sorl options.
*args: (Optional) Default serializers.ImageField arguments.
**kwargs: (Optional) Default serializers.ImageField keyword
arguments.
For a description of sorl geometry strings and additional sorl options,
please see https://sorl-thumbnail.readthedocs.org/en/latest/examples.html?highlight=geometry#low-level-api-examples
""" # NOQA
self.geometry_string = geometry_string
self.options = options
super(HyperlinkedSorlImageField, self).__init__(*args, **kwargs)
def to_representation(self, value):
"""
Perform the actual serialization.
Args:
value: the image to transform
Returns:
a url pointing at a scaled and cached image
"""
if not value:
return None
image = get_thumbnail(value, self.geometry_string, **self.options)
try:
request = self.context.get('request', None)
return request.build_absolute_uri(image.url)
except:
try:
return super(HyperlinkedSorlImageField, self).to_representation(image)
except AttributeError: # NOQA
return super(HyperlinkedSorlImageField, self).to_native(image.url) # NOQA
to_native = to_representation
不幸的是,有时在我的服务器上失败,并显示以下错误:
OSError: image file is truncated (44 bytes not processed)
我该怎么做才能解决此问题?我想图像有点不完整。但是我可以毫无问题地查看它们吗?