Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid NotImplementedError "Only tempfile.TemporaryFile is available for use" in django on Google App Engine?

I'm using Django 1.1 on Google App Engine through use_library. No Django GAE helper, Django non-rel or similar tools are used here. Django handles URLs routing, forms validation etc., but I'm using pure App Engine models.

In one of my Django forms there is a FileField, which from time to time, seems to call django.core.files.uploadedfile.TemporaryUploadedFile. This class then uses tempfile.NamedTemporaryFile and this results in App Engine raising:

File "/base/python_runtime/python_dist/lib/python2.5/tempfile.py", line 45, in PlaceHolder
   raise NotImplementedError("Only tempfile.TemporaryFile is available for use")

Trying to solve this problem I took uploadedfile module from Google App Engine Helper for Django (which doesn't use NamedTemporaryFile) saved it as gae_uploadedfile.py in application directory and in my _djangomain.py_ file I added:

from google.appengine.dist import use_library
use_library('django', '1.1')
(...)
import gae_uploadedfile
django.core.files.uploadedfile = gae_uploadedfile

djangomain.py is a file where i redirect all urls - in app.yaml I have:

- url: /.*
  script: djangomain.py

But it didn't help, I still get this exception. What am I doing wrong, is there other solution to avoid this error while using FileField from django.forms?

like image 387
Pawel Markowski Avatar asked Aug 27 '10 16:08

Pawel Markowski


1 Answers

You need to update the settings.py file with the following to change the default Django behaviour:

# only use the memory file uploader, do not use the file system - not able to do so on
# google app engine
FILE_UPLOAD_HANDLERS = ('django.core.files.uploadhandler.MemoryFileUploadHandler',)
FILE_UPLOAD_MAX_MEMORY_SIZE = 2621440 # the django default: 2.5MB

More info here:FILE_UPLOAD_MAX_MEMORY_SIZE and upload-handlers

If you are uploading images you will be restricted by the 1MB quotas for image transformation etc.. Quotas_and_Limits

like image 82
jonmiddleton Avatar answered Oct 13 '22 06:10

jonmiddleton