使用self.image.width的Django模型方法性能

普罗米修斯

我有以下模型方法。

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

何时在方法中使用self,&self和&mut self?

在Django的模型queryset中使用self.request.user

从模板使用Django模型方法

为什么要对Django 1.7+使用__unicode __(self)方法?

使用 self 变量作为 python 方法参数

使用self。*作为方法的默认值

使用self。*作为方法的默认值

何时在模块方法中使用self

在类的多种方法中使用self

在方法中使用“self”关键字

在Django模型中编写def __str __(self)的更好方法

Django模型管理器是否需要使用= self._db

PIL.Image和PIL.Image.Image之间的混淆以及使用它们的正确方法?

使用模型方法作为Django模型的默认值?

在initialize和其他类方法中使用self时,self如何工作?

Django在self.object中使用related_name

django测试使用的正确模板:self.assertTemplateUsed()

在 Django 中使用不带“def __init__”的“self”实例

使用“self”从静态方法调用非静态方法

为什么我们必须使用 self.rect 和 self.image 来确定 Sprite 上的 Rect 和 Surf?

Ruby-实例方法:为什么我可以不使用self而使用getter,而只能使用self进行setter

无法在php中的静态方法中使用self

在方法构造中使用 None 和 self 关键字

如何避免在 Python 类方法中使用 self?

在Python中使用类方法时,可以解构`self`吗?

在类定义中的方法名称中明确使用`self`

如何使用包含返回对Self的引用的方法的特征对象?

PHPUnit-使用$ this还是self作为静态方法?

如何在成员方法关闭中使用struct self