_post_save() problem with an empty file field

6 views
Skip to first unread message

sam

unread,
May 4, 2006, 6:08:23 PM5/4/06
to Django users
Django version 0.91. I was trying to generate a thumbnail for an image
upload. The image is a FileField. I want to generate thumbnail in
_post_save() of the model object. The code is something like the
following:

class Item(meta.Model):
...
file = meta.FileField(upload_to="files",blank=True)
...

def _post_save(self):
path = self.get_file_filename()
im = Image.open(path)
im.thumbnail((128,128))
(p1,p2) = path.split('.')
im.save(p1+"_tn."+p2)

What I found is everytime I do "add item" in admin interface,
self.get_file_filename() returns the path of MEDIA_ROOT, not the full
path of the image file uploaded. How to solve this problem? Thanks in
advance.

Adrian Holovaty

unread,
May 4, 2006, 10:01:59 PM5/4/06
to django...@googlegroups.com

Hey Sam,

This is happening because of a bug -- if you use a FileField,
_post_save() is called twice: once for the object itself, then again
to save the file-upload information.

I haven't tested this, but you may be able to hack around the problem
by checking whether self.file has been set. If it's been set, then do
the image resizing, etc. If it hasn't been set yet, it's the first
time through _post_save() and you shouldn't do anything.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com

Reply all
Reply to author
Forward
0 new messages