How to store and retrieve file in mongoengine?

Posted by Seiverence on Stack Overflow See other posts from Stack Overflow or by Seiverence
Published on 2012-09-10T14:00:47Z Indexed on 2012/09/11 3:38 UTC
Read the original article Hit count: 106

Filed under:
|

I am attempting to store and retrieve file within mongodb, but am having issues with the retrieval.

class Animal(Document):
    genus = StringField()
    family = StringField()
    photo = FileField()

def get_file():
    marmot = Animal.objects(genus='Marmota').first()
    photo = marmot.photo.read()
    content_type = marmot.photo.content_type
    print marmot.family # Prints out "Sciuridae"
    print content_type # Gives me an error, as content_type is "None"

def save_file():
    marmot = Animal(
        genus='Marmota', 
        family='Sciuridae')
    marmot_photo = open('marmot.jpg', 'r') 
    marmot.photo = marmot_photo       
    marmot.photo.content_type = 'image/jpeg'  
    marmot.save()

When I check mongodb, it appears the document does save after the save_file, but when I call get_file, it appears the content_type is "None"? Am I saving and retrieving the file correctly? If not, whats wrong with the code?

NOTE: The issue appears only to occur in the Windows environment. When run on linux, it works fine... very confused.

© Stack Overflow or respective owner

Related posts about python

Related posts about mongoengine