Django Custom image upload field with dynamic path
Asked Answered
L

3

1

I'm trying to create my own field type for uploading images, it won't be used in the django admin panel. I imagine the syntax would probably look something like:

photo = CustomImageField(upload_to='images', width="200", height="200")

I want to be able to set the parameters in the field when I upload and save an image. I've looked through the django documentation but I can't find anything about how to do this. So basically, I want to be able to control where the image is uploaded to and the width and height parameters when the upload form is processed and the object is saved.

I am using Django 1.2

Any help would be greatly appreciated :)

Lang answered 27/1, 2011 at 10:25 Comment(0)
S
2

Your can change the storage of a FileField using the location property.

form_name.photo.storage.location = upload_dir_path      

It works for the FileField and maybe it will also work for your custom image field if you inherit the Django's ImageField.

Selfcontrol answered 27/1, 2011 at 14:4 Comment(0)
M
3

You don't need a custom field for this. As the documentation shows, upload_to can be a callable, which is passed the instance and returns a path which determines where the field is saved.

I'm not sure what you mean about the width and height parameters. These are calculated from the image in any case. If you specify height_field and width_field in the field definition, the corresponding fields will be filled with the values of the image's height and width when the image is uploaded.

Municipality answered 27/1, 2011 at 11:35 Comment(1)
I'll look into 'upload_to'. What I want to do is be able to specify the width and height of the upload image individually. I understand I can hijack the fields save method to do this, but I want to be able to specific the dimensions each time an image is uploaded as they will change depending on the 'album' they are being uploaded to.Lang
S
2

Your can change the storage of a FileField using the location property.

form_name.photo.storage.location = upload_dir_path      

It works for the FileField and maybe it will also work for your custom image field if you inherit the Django's ImageField.

Selfcontrol answered 27/1, 2011 at 14:4 Comment(0)
A
0

all info for uploading:

http://docs.djangoproject.com/en/dev/topics/files/

and info on resizing

resize image on save

i'll hope it helps

EDIT: all you need i think is: http://code.google.com/p/django-stdimage/ but i don't know if it works on current version.

Alimentary answered 27/1, 2011 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.