Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Every mongoengine field can be deep copied, except for file fields.
from copy import deepcopy
from mongoengine import *
from pymongo import Connection
class A(Document):
binary_field = BinaryField()
boolean_field = BooleanField()
date_time_field = DateTimeField()
decimal_field = DecimalField()
dict_field = DictField()
email_field = EmailField()
embedded_document_field = EmbeddedDocumentField(EmbeddedDocument)
file_file = FileField()
float_field = FloatField()
generic_reference_field = GenericReferenceField()
geo_point_field = GeoPointField()
int_field = IntField()
list_field = ListField(boolean_field)
object_id_field = ObjectIdField()
reference_field = ReferenceField(Document)
sorted_list_field = SortedListField(boolean_field)
string_field = StringField()
url_field = URLField()
db = 'tmp-deepcopy'
a = A()
for field in sorted(a._fields):
print field, a[field], deepcopy(a[field])
except TypeError as e:
print 'FAILED'
print '\t', e
binary_field None None
boolean_field None None
date_time_field None None
decimal_field None None
dict_field {} {}
email_field None None
embedded_document_field None None
file_file <mongoengine.fields.GridFSProxy object at 0x9930e2c> FAILED
'Collection' object is not callable. If you meant to call the '__deepcopy__' method on a 'Collection' object it is failing because no such method exists.
float_field None None
generic_reference_field None None
geo_point_field None None
id 4d8d039e8b61460f65000000 4d8d039e8b61460f65000000
int_field None None
list_field [] []
object_id_field None None
reference_field None None
sorted_list_field [] []
string_field None None
url_field None None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment