0

I am attempting to get mongodb + gridfs working for django so that I can serve files out of django, but upon programmatically creating a new file, I get this error. I'm not sure which documents to follow as it appears some of them are out of date.

Here is my settings.py:

DATABASES = {
    'files': {
        'ENGINE': 'django_mongodb_engine',
        'NAME': 'files',
        'USER': 'files',
        'PASSWORD': 'files',
        'HOST': 'localhost',
        'PORT': 27017,
        'SUPPORTS_TRANSACTIONS': False,
    }
}


DEFAULT_FILE_STORAGE = 'mongo_storage.storage.GridFSStorage'
GRIDFS_DATABASE = 'files'

And, in my model, I'm simply using a file field:

data = models.FileField(upload_to="/tmp/uploads")

Then, I'm creating a new file ...:

internal_file = File()
internal_file.data.save(checksum, open(path).read())

That is when I get the attribute error.

Do I need to configure my model differently, not call the save() method explicitly?

karthikr
  • 97,368
  • 26
  • 197
  • 188
Walter
  • 1,290
  • 2
  • 21
  • 46
  • You need to wrap your content in ContentFile, or wrap the file handler in a Django File object when calling save(). Look at this answer: http://stackoverflow.com/a/7515224/1530272 – Sohan Jain Apr 02 '14 at 16:29
  • I tried that before and again, I get this error: value too long for type character varying(32). I'm not sure if that is a positive step or a side step. – Walter Apr 02 '14 at 17:23
  • positive step: the path you're uploading to has to be less than 32 chars according to your db constraint. either up the max charsize on the file name field, or use a shorter file name – Sohan Jain Apr 02 '14 at 17:32
  • Hi Sohan, is that for the mongodb name field or what? I tried changing my constraints for the model as well as changing the filename itself. – Walter Apr 02 '14 at 18:26

1 Answers1

0

I figured it out, I just had to do this:

image.data.save(image.checksum, File(open(path)))

Prior to doing that, I was just passing in a regular python file which was the problem.

Walter
  • 1,290
  • 2
  • 21
  • 46