juliomalegria / django-chunked-upload

Upload large files to Django in multiple chunks, with the ability to resume if the upload is interrupted.
MIT No Attribution
218 stars 73 forks source link

switch upload_to to a callable #19

Closed Hannu1 closed 7 years ago

Hannu1 commented 8 years ago

I use user id in the chunked upload filepath. I couldn't figure out a way to cleanly replace upload_to=generate_filename without tampering with the code (if there is one feel free to close the issue). I ended up cloning the project and made some other changes as well, but this is something I think could be beneficial for many users.

Basic idea is: file = models.FileField(max_length=255, upload_to=UPLOAD_PATH, storage=STORAGE)

Then users are more free to alter the upload_to path.

For example i did this in my settings.py: def generate_filename(instance, filename): filename = str(instance.upload_id) + '.part' return "{0}/{1}".format(instance.user.id, filename)

CHUNKED_UPLOAD_PATH = generate_filename

For it to be backwards compatible, a new setting could be added be where the default implementation is the old generate_filename.

tcztzy commented 8 years ago

I don't know why but in Django 1.9, the following code will work, same code in 1.8 and former would not work.

SomeModel._meta.get_field("your_file_field").upload_to = your_own_upload_to_string_or_function

juliomalegria commented 8 years ago

That's a great idea for improvement @Hannu1. I'll work on that on my free time :)

juliomalegria commented 7 years ago

This was implemented in af99eb9. Thanks for the good idea! 👍