Allow directories in filename argument to save_<fieldname>_file
|Reported by:||Owned by:||Philippe Raoult|
|Cc:||Triage Stage:||Design decision needed|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Currently, all files associated with FileFields are uploaded into a single directory specified by MEDIA_ROOT and upload_to. There is no way to specify directories on a per-file basis when files are uploaded. With this patch, if the filename argument to save_<fieldname>_file specifies a directory as well as a filename, the file will be uploaded to that directory under the MEDIA_ROOT/upload_to directory. Example:
class File(meta.Model): file = meta.FileField(upload_to='baseDir') name = meta.CharField(maxlength=100)
newFile = files.File(name='my file') newFile.save_file_file('some/sub/directory/myFile.txt', 'Some text content')
The above lines will create a file at: <MEDIA_ROOT>/baseDir/some/sub/directory/myFile.txt.
This shouldn't change how most file uploading (including the admin app) works now, because normally just a filename without any directories is passed to save_<fieldname>_file. However, it allows custom view code or FileField subclasses (each of which will call save_<fieldname>_file) to save files to specific locations easily.
Change History (6)
comment:2 Changed 9 years ago by
|Owner:||changed from nobody to Philippe Raoult|
|Status:||new → assigned|