Created
October 19, 2011 19:09
-
-
Save tfausak/1299339 to your computer and use it in GitHub Desktop.
Shows MongoEngine's peculiar handling of ListField of FileFields.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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