7

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.

mimo
  • 2,469
  • 2
  • 28
  • 49

1 Answers1

11

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',]
ohrstrom
  • 2,890
  • 2
  • 20
  • 34
  • 7
    I need to tune this on specific fields only. Can that be applied to my ``file`` field only? – mimo Jul 14 '16 at 03:08