Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django Storages S3获取每个查询中的所有图像_Django_Amazon S3_Boto - Fatal编程技术网

Django Storages S3获取每个查询中的所有图像

Django Storages S3获取每个查询中的所有图像,django,amazon-s3,boto,Django,Amazon S3,Boto,我将我的项目设置为使用带有S3Boto后端的django存储来使用AmazonS3。我的很多模型都有上传到S3的ImageFields,并且工作正常 当我尝试使用all()或filter()进行查询集时,问题就出现了。对于每个请求,包括django admin请求,django都从服务器获取查询集中的每个图像 我所做的唯一更改是在ImageField中添加width\u field和height\u field,以保存高度和宽度,因此我不必从S3检索图像来获取此信息 如果有人知道为什么会发生这种

我将我的项目设置为使用带有S3Boto后端的django存储来使用AmazonS3。我的很多模型都有上传到S3的ImageFields,并且工作正常

当我尝试使用all()或filter()进行查询集时,问题就出现了。对于每个请求,包括django admin请求,django都从服务器获取查询集中的每个图像

我所做的唯一更改是在ImageField中添加width\u fieldheight\u field,以保存高度和宽度,因此我不必从S3检索图像来获取此信息


如果有人知道为什么会发生这种情况,或者知道如何调试这种情况,这将非常有用。我一直在看S3Boto的代码,不知道该检查什么

这些对图像文件的访问可能来自
django.db.models.fields.files.ImageField.update\u dimension\u fields

下面是它的描述:

"""
Updates field's width and height fields, if defined.

This method is hooked up to model's post_init signal to update
dimensions after instantiating a model instance.  However, dimensions
won't be updated if the dimensions fields are already populated.  This
avoids unnecessary recalculation when loading an object from the
database.

Dimensions can be forced to update with force=True, which is how
ImageFileDescriptor.__set__ calls this method.
"""    
在你的情况下,我会做的是扮演我们自己的宽度字段和高度字段的角色,并在上传图像时只更新它们一次。下面是我将如何实现它(尚未在S3的生产环境中测试,但使用本地存储):

class ImageCovered(models.Model):
    def __init__(self, *args, **kwargs):
        # read and store original image data
        super(ImageCovered, self).__init__(*args, **kwargs)
        self.__original_cover_image = self.cover_image

    cover_image = models.ImageField(upload_to=get_upload_path, max_length=200, null=True, blank=True,)
    # We should not use ImageField's width_field nor height_field here, since it might call S3 files.
    # Instead, we implement our own fields and update it once only upon image saving.
    cover_image_height = models.PositiveSmallIntegerField(default=0)
    cover_image_width = models.PositiveSmallIntegerField(default=0)

    def save(self):
        super(ImageCovered, self).save()
        # Check if cover_image has been changed
        if self.__original_cover_image != self.cover_image :
            self.cover_image_height = self.cover_image.height
            self.cover_image_width = self.cover_image.width
            self.__original_cover_image = self.cover_image #replace original data
            super(ImageCovered, self).save(update_fields=('cover_image_height', 'cover_image_width',))