8

I have a model.FileField(upload_to='%Y/%m/%d') field. This works great; however, I want to rename the file based on the context of the user uploading the file before it is saved. Is there a way of modifying the request object before it saved to accomplish this?

I have come across people with similar issues but the answers always point back to the Django documentation. I have tried figuring this out using the documentation but can't. Can someone provide some code to show hot to solve this?

Thanks in advance.

Przemek
  • 459
  • 7
  • 16
  • 2
    possible duplicate of [In django changing the file name of uploading file](http://stackoverflow.com/questions/2680391/in-django-changing-the-file-name-of-uploading-file) – Fernando Freitas Alves Jun 30 '13 at 03:18

1 Answers1

0

you can use function for upload_to value with instance and filename inputs and return path and file name

for example:

def upload_to_func(instance, filename):
    now = datetime.now()
    return os.path.join(str(now.year), str(now.month), str(now.day), filename)



field_x = model.FileField(upload_to=upload_to_func)

you can change path and filename in function