Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Shows MongoEngine's peculiar handling of ListField of FileFields.
"""Python v2.7.1, MongoDB v1.8.2, PyMongo v1.10, MongoEngine v0.4
"""
from mongoengine import connect, Document, FileField, ListField
from mongoengine.base import ValidationError
from mongoengine.fields import GridFSProxy
from pymongo import Connection
from time import time
# Start with a clean slate.
database = 'temporary-{0}'.format(int(time()))
connection = Connection()
connection.drop_database(database)
connect(database)
# Define a problematic document.
class Example(Document):
field = ListField(FileField())
# This document becomes invalid upon saving.
example = Example(field=[GridFSProxy()])
example.save()
example.reload()
try:
example.save()
except ValidationError as error:
print error # Invalid ListField item (None)
# This document stays valid.
file_ = GridFSProxy()
file_.put('')
example = Example(field=[file_])
example.save()
example.reload()
example.save()
# Clean up after ourselves.
connection.drop_database(database)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment