Search code examples
pythondjangofile-uploaddjango-formsfilefield

Django Custom image upload field with dynamic path


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 :)


Solution

  • 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.