Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileField: force using TemporaryUploadedFile

When uploading a file for a field such as this one:

file = FileField(upload_to='/path/')

Django uses either an InMemoryUploadedFile or a TemporaryUploadedFile. The latter is stored on the disk and its file name can be accessed with the temporary_file_path property. The storage choice depends on the file size.

How can I override this behaviour and always upload as a TemporaryUploadedFile for this model field?

The reason I am asking this is because I need to write a validator that uses an external library that can only take file paths as input, no data streams.

like image 706
mimo Avatar asked Jul 13 '16 08:07

mimo


1 Answers

By default the upload handlers are:

[
    'django.core.files.uploadhandler.MemoryFileUploadHandler',
    'django.core.files.uploadhandler.TemporaryFileUploadHandler',
]

And files smaller than 2.5MB are handled with MemoryFileUploadHandler.

So just say in your settings:

FILE_UPLOAD_HANDLERS = ['django.core.files.uploadhandler.TemporaryFileUploadHandler',]
like image 110
ohrstrom Avatar answered Nov 11 '22 11:11

ohrstrom