问题
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 :)
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/4815150/django-custom-image-upload-field-with-dynamic-path