Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the file name of an uploaded file in Django?

Is it possible to change the file name of an uploaded file in django? I searched, but couldn't find any answer.

My requirement is whenever a file is uploaded its file name should be changed in the following format.

format = userid + transaction_uuid + file_extension 

Thank you very much...

like image 813
Software Enthusiastic Avatar asked Apr 21 '10 04:04

Software Enthusiastic


People also ask

How do I change the filename in Django?

you can overwrite file name by overwriting _name value of file object.

How do I rename an uploaded file?

You can simply change the name of the file by changing the name of the file in the second parameter of move_uploaded_file . $temp = explode(".", $_FILES["file"]["name"]); $newfilename = round(microtime(true)) .

How do I rename an image in Django?

Depending on the OS, . , > , : , and backslashes may be also used. Possible duplicate of How to change the file name of an uploaded file in Django?, Choose the filename of an uploaded file with Django, Django File Upload and Rename, Rename file on upload to admin using Django, etc.


2 Answers

How are you uploading the file? I assume with the FileField.

The documentation for FileField.upload_to says that the upload_to field,

may also be a callable, such as a function, which will be called to obtain the upload path, including the filename. This callable must be able to accept two arguments, and return a Unix-style path (with forward slashes) to be passed along to the storage system. The two arguments that will be passed are:

"instance": An instance of the model where the FileField is defined. More specifically, this is the particular instance where the current file is being attached.

"filename":The filename that was originally given to the file. This may or may not be taken into account when determining the final destination path.

So it looks like you just need to make a function to do your name handling and return the path.

def update_filename(instance, filename):     path = "upload/path/"     format = instance.userid + instance.transaction_uuid + instance.file_extension     return os.path.join(path, format) 
like image 152
monkut Avatar answered Sep 29 '22 22:09

monkut


You need to have a FileField with the upload_to that calls to a callback, see [1]

Your callback should call a wrapper method which gets an instance as one of the params and filename as the other. [2]

Change it the way you like and return the new path [3]

1. LOGIC

FileField(..., upload_to=method_call(params),....) 

2. define method

def method_call(params):     return u'abc' 

3. Wrapper:

def wrapper(instance, filename):     return method 

this is the rapper method that you need for getting the instance.

def wrapper(instance, filename): ... Your logic ... return wrapper 

Complete Code

def path_and_rename(path, prefix):     def wrapper(instance, filename):         ext = filename.split('.')[-1]         project = "pid_%s" % (instance.project.id,)         # get filename         if instance.pk:             complaint_id = "cid_%s" % (instance.pk,)             filename = '{}.{}.{}.{}'.format(prefix, project, complaint_id, ext)         else:             # set filename as random string             random_id = "rid_%s" % (uuid4().hex,)             filename = '{}.{}.{}.{}'.format(prefix, project, random_id, ext)             # return the whole path to the file         return os.path.join(path, filename)      return wrapper 

Call to Method

sales_attach = models.FileField("Attachment", upload_to=path_and_rename("complaint_files", 'sales'), max_length=500,                                 help_text="Browse a file") 

Hope this helps. Thanks.

like image 32
A.J. Avatar answered Sep 29 '22 22:09

A.J.