Updating path and filename of ImageFieldFile

1,366 views
Skip to first unread message

mcordes

unread,
Aug 13, 2008, 11:22:15 PM8/13/08
to Django users
Hello,

When uploading files, I'd like to upload them to a temporary location
and then after post processing move them to a different directory/
file. I had this working with the pre-filestorage code, but I'm not
sure what the correct procedure should now be.

My model looks like this:

> class MyModel(models.Model):
> image = models.ImageField(upload_to="images/tmp")


An uploaded file would then get stored here (relative to my media
root):

> images/tmp/somefile.png


I'd like to be able to modify the associated MyModel instance so its
image field now has a name of

> images/processed/XXX.png


I've tried using FieldFile.save as in

> myModelInstance.image.save('new/path/new.png', File(file('path to processed image file')))

and this mostly seems to work, but it throws out the 'new/path' part
of the name argument and my image ultimately gets stored here:

> images/tmp/new.png

rather than where I want it: 'images/processed/new.png'.

What am I missing? Am I attacking this all wrong? What are your
suggestions?

-Matt



Andy Lei

unread,
Aug 14, 2008, 12:18:07 AM8/14/08
to django...@googlegroups.com
If the only post processing you need to do is changing the filename, you should pass a callable (like a function) as the upload_to parameter.  

for example:

def get_upload_path(instance, filename):
    return '/path/you/want/' + instance.name

class MyModel(models.Model):

    image = models.ImageField(upload_to=get_upload_path)



Check out the documentation here:



If you need to do other post processing, I think you want to take advantage of the pre / post save signals, or override save method on your model.  Just do the post processing in place though; i can't see a reason why you'd need to save it twice.

-Andy

mcordes

unread,
Aug 14, 2008, 5:58:09 AM8/14/08
to Django users
Thanks Andy,

Using the callable for upload_to is pretty interesting, but I don't
think it's usable for my purposes because I want the image file's name
to be XXX.png where XXX is the primary key for the model which, as the
documentation warns, may not be set when the upload_to callable gets
called.

I was previously doing this in the model's save method, but as I
mentioned after the filestorage change it's not clear how to modify
the path of an existing ImageFieldFile

As for my image processing it's pretty simple and consists of:

1. resize image
2. changing format to png
3. applying relatively simple transformations on the image
4. then saving it in a different directory with a name based on the
model's id, something like /path/i/want/<MyModel.id>.png

-Matt


On Aug 14, 12:18 am, "Andy Lei" <andy...@gmail.com> wrote:
> If the only post processing you need to do is changing the filename, you
> should pass a callable (like a function) as the upload_to parameter.
> for example:
>
> def get_upload_path(instance, filename):
>     return '/path/you/want/' + instance.name
>
> class MyModel(models.Model):
>
>     image = models.ImageField(upload_to=get_upload_path)
>
> Check out the documentation here:
>
> http://www.djangoproject.com/documentation/model-api/#filefield
>
> If you need to do other post processing, I think you want to take advantage
> of the pre / post save signals, or override save method on your model.  Just
> do the post processing in place though; i can't see a reason why you'd need
> to save it twice.
>
> -Andy
>

Juanjo Conti

unread,
Oct 3, 2008, 5:43:57 PM10/3/08
to django...@googlegroups.com
Matt, I'd like to resize an uploaded image, while uploading i think...
before it gets saved to the hard disk. How did you do it? In pre 1.0
version I redefined _save_FIELD_file from my class.

Thanks,

2008/8/14 mcordes <cordes....@gmail.com>:

--
Juanjo Conti

Jon Biddle

unread,
Oct 21, 2008, 1:23:06 PM10/21/08
to Django users
This code should work with Django 1.0 :

from django.db import models
from PIL import Image

class Photo(models.Model):
related_entry_id = models.ForeignKey(MyBlogEntryModel) # Use your
model object
photo = models.ImageField(upload_to="my_upload_path/") # Point to
your upload directory (No leading slash will point to a subfolder of
your media root)

def __str__(self):
return "%s" % (self.photo.name)

def _save_FIELD_file(self, field, filename, raw_contents,
save=True):
filename = "%s_%s" % (self.related_entry_id, filename)

super(Photo, self)._save_FIELD_file(field, filename,
raw_contents, save)

def save(self, size=(800, 800)):

if not self.id and not self.photo:
return

super(Photo, self).save()

filename = self.photo.path
image = Image.open(filename)
image.thumbnail(size, Image.ANTIALIAS)
image.save(filename)


On Oct 3, 5:43 pm, "Juanjo Conti" <jjco...@gmail.com> wrote:
> Matt, I'd like to resize an uploaded image, while uploading i think...
> before it gets saved to the hard disk. How did you do it? In pre 1.0
> version I redefined _save_FIELD_file from my class.
>
> Thanks,
>
> 2008/8/14 mcordes <cordes.matt...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages