* Dirk Eschler wrote, On 13.02.2007 11:12:
> Hello,
> 
> i use ImageWithThumbnailField to upload screenhots. In the save method i want 
> to set some additional model fields, like filesize and width/height of the 
> uploaded image. The problem is, that self.image isn't defined at this point, 
> the same entry has to be saved twice in the admin.

pre_save signal at which image rename/processing is done is fired up from 
django.db.Model.save() so you must call it 
first if you want to have your image renamed.

OTOH, self.image (before calling save) is populated with original file name 
(IIRC when I used to manually create 
thumbnails in the save() method). More precisely it is populated at the point 
of dispatching pre_save signal which is 
the first line in the base model save() (look at the source of 
ImageWithThumbnailField).

> 
> Checking for self.image (like i read in another thread) doesn't work for me. 
> Is there any way to get around this problem?
> 
> # -----------------------------
> class Screenshot(models.Model):
>     image    = ImageWithThumbnailField(upload_to='upload/screenshots/')

image    = ImageWithThumbnailField(upload_to='upload/screenshots/', 
width_field='width', height_field='height')

>     filesize = models.PositiveIntegerField(null=True, blank=True, 
> editable=False)

.get_image_size()

>     width    = models.PositiveSmallIntegerField(null=True, blank=True, 
> editable=False)

.get_image_width()

>     height   = models.PositiveSmallIntegerField(null=True, blank=True, 
> editable=False)

.get_image_height()

> 
>     def get_image_path(self):
>         return '%s/%s' % (settings.MEDIA_ROOT, self.image)

.get_image_filename().

> 
>     def save(self):
>         super(Screenshot, self).save()
>         if self.image:
>             self.filesize = getsize(self.get_image_path())
>             img = Image.open(self.get_image_path())
>             self.width = img.size[0]
>             self.height = img.size[1]
> 

Or if you want to store filesize/whatever in the database:

def save(self):
        super(Screenshot, self).save()
        self.filesize = getsize(self.get_image_filename())
        super(Screenshot, self).save() # save again to store filesize

It saves twice to the database, but IMHO this is not a big problem.

I presumed that `image` field is required so that never be a case of empty 
`image` field submission.

Also you can subclass ImageWithThumbnailField and override _save() like this 
(untested):

class ScreenshotImageField(ImageWithThumbnailField):
        def _save(self, instance=None):
                super(ScreenshotImageField, self)._save(instance) # do the 
renaming stuff
                setattr(instance, 'filesize', getsize(getattr(instance, 
'get_%s_filename' % self.attname)())

This way you can avoid need for overriding .save() method.

I hope that helps.

-- 
Nebojša Đorđević - nesh, ICQ#43799892
Studio Quattro - Niš - Serbia
http://studioquattro.biz/ | http://trac.studioquattro.biz/djangoutils/
Registered Linux User 282159 [http://counter.li.org]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to