Friday, 15 January 2010

amazon s3 - Django Storages S3 gets all images in every query -



amazon s3 - Django Storages S3 gets all images in every query -

i setup project utilize amazon s3 using django-storages s3boto backend. somo of models have imagefields uploaded s3 , it's working fine.

the problem comes when seek queryset all() or filter(). every request, including django-admin ones, django getting every image in query set server.

the alter made adding width_field , height_field imagefield save height , width dont have retrieve image s3 info.

it helpful if has thought on why happening or debug this. i've been looking around @ code of s3boto , i'm not sure check.

these accesses image files might originate django.db.models.fields.files.imagefield.update_dimension_fields.

here's it's description says:

""" updates field's width , height fields, if defined. method hooked model's post_init signal update dimensions after instantiating model instance. however, dimensions won't updated if dimensions fields populated. avoids unnecessary recalculation when loading object database. dimensions can forced update force=true, how imagefiledescriptor.__set__ calls method. """

what in case role our own width_field , height_field , update them 1 time upon image uploading. here how implement (not tested on production s3 yet, work local storage):

class imagecovered(models.model): def __init__(self, *args, **kwargs): # read , store original image info 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,) # should not utilize imagefield's width_field nor height_field here, since might phone call s3 files. # instead, implement our own fields , update 1 time 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 info super(imagecovered, self).save(update_fields=('cover_image_height', 'cover_image_width',))

django amazon-s3 boto

No comments:

Post a Comment