我有以下模型方法。
def make_image_width(self):
"""
Returns image width of the object.
"""
if self.image:
return self.image.width
return "null"
当我在序列化程序中运行此程序时,需要2分钟才能呈现100个对象。
class ImageSerializer(serializers.HyperlinkedModelSerializer):
image_width = serializers.ReadOnlyField(source='make_image_width')
class Meta:
model = Image
fields = (
'id','image_width',)
为什么self.image.width
这么慢?我该如何改善呢?
您的方法读取图像文件以获取宽度。
您应该在模型中创建一个整数字段,并使用ImageField的width_field
属性指向它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句