MEDIA_ROOT is the directory where you want the files to go, MEDIA_URL should be a URL that leads to the same directory path.
The upload_to parameter can either be a directory in this directory, so upload_to = "foo" will be in the "foo" subdirectory of MEDIA_ROOT.
Or it could be a function that takes an instance of the image field and the proposed base file name and returns the real file name that you want to use. So,
upload_to=get_photo_path
and
def get_photo_path(instance, filename): if not filename: return "" exts = re.search('[.]([^.]*)$',filename) if exts is None: ext = '' else: ext = '.'+exts.group(1) return "newname%s" % (ext)
Rename the file, but save it. Please note that the "new name". it will still be in the MEDIA_ROOT directory - you do not need to return / usr / whatever / something / foo / newname. ". It already broadcasts MEDIA_ROOT for you.
Update:
FYI, nice to know about
<modelobject>.<imagefieldname>.field.generate_filename( <modelobject>, proposed_name )
Here's how you can create a partial path that the image will follow from outside the model. You need to manually add MEDIA_ROOT to this to make it an absolute path.
source share