Filefield uploads in django admin

109 views
Skip to first unread message

Andy Cheesman

unread,
Jun 6, 2017, 10:32:16 AM6/6/17
to Django users
Hi everyone

I've a problem using filefields in the admin module.I've a standard model which is has a FileField (which I use to upload pdfs)
When I'm in the administration console and attempt to upload a pdf, the filename is modified from name.pdf to name_XXXX.pdf. I would like the uploaded file to keep the original name!
I've looked around at CRSF issues with django and they usually suggest @csrf_exempt in the views.py, which won't work in this situation. 

I know this is information light but I didn't want to spam the list with lots of pointless information - If anyone has any starting points, I would happily provide further useful information

Yours 

Andy 


Tim Graham

unread,
Jun 6, 2017, 11:59:06 AM6/6/17
to Django users
The default behavior of FileSystemStorage is, "If a file with name already exists, an underscore plus a random 7 character alphanumeric string is appended to the filename before the extension." Do you want to always overwrite existing files if a file with that name already exists? That's a bit dangerous, but you can accomplish it by writing a custom storage.

https://docs.djangoproject.com/en/dev/ref/files/storage/#django.core.files.storage.Storage.get_available_name

Melvyn Sopacua

unread,
Jun 6, 2017, 8:47:11 PM6/6/17
to django...@googlegroups.com

On Tuesday 06 June 2017 08:59:06 Tim Graham wrote:

> The default behavior of FileSystemStorage is, "If a file with name

> already exists, an underscore plus a random 7 character alphanumeric

> string is appended to the filename before the extension." Do you want

> to always overwrite existing files if a file with that name already

> exists? That's a bit dangerous, but you can accomplish it by writing

> a custom storage.

 

Or you can use a callable to upload_to if you don't care about being storage backend (filesystem) agnostic.

 

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages