Created
April 21, 2011 03:41
-
-
Save g-k/933670 to your computer and use it in GitHub Desktop.
mongokit uuid support
This file contains hidden or 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
# result of running mongokit_uuid.py with "default_values = { 'my_uuid' : uuid.uuid4 }" | |
> python mongokit_uuid.py | |
Traceback (most recent call last): | |
File "mongokit_uuid.py", line 22, in <module> | |
myid.save() | |
File "/Library/Python/2.6/site-packages/mongokit/document.py", line 398, in save | |
self.validate(auto_migrate=False) | |
File "/Library/Python/2.6/site-packages/mongokit/document.py", line 247, in validate | |
super(Document, self).validate() | |
File "/Library/Python/2.6/site-packages/mongokit/schema_document.py", line 348, in validate | |
self._validate_doc(self, self.structure) | |
File "/Library/Python/2.6/site-packages/mongokit/schema_document.py", line 585, in _validate_doc | |
self._validate_doc(doc[key], struct[key], new_path) | |
File "/Library/Python/2.6/site-packages/mongokit/schema_document.py", line 537, in _validate_doc | |
path, struct.__name__, type(doc).__name__)) | |
File "/Library/Python/2.6/site-packages/mongokit/schema_document.py", line 519, in _raise_exception | |
raise exception(message) | |
mongokit.schema_document.SchemaTypeError: my_uuid must be an instance of Binary not UUID |
This file contains hidden or 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
import uuid | |
import mongokit | |
import pymongo | |
from pymongo.binary import Binary, UUID_SUBTYPE | |
class HasUuid(mongokit.Document): | |
__database__ = 'test' | |
__collection__ = 'uuids' | |
structure = { 'my_uuid' : Binary } | |
# default_values = { 'my_uuid' : uuid.uuid4 } | |
default_values = { 'my_uuid' : lambda : Binary(str(uuid.uuid4()), UUID_SUBTYPE) } | |
authorized_types = mongokit.Document.authorized_types + [uuid.UUID] | |
#skip_validation = True | |
if __name__ == '__main__': | |
c = mongokit.Connection() | |
c.register([HasUuid]) | |
myid = c.test.HasUuid() | |
myid.save() | |
print myid | |
mysecondid = c.test.HasUuid() | |
print mysecondid | |
mysecondid.save() | |
for i in c.test.ids.find(): | |
print i | |
# runs with 'my_uuid' : lambda : Binary(str(uuid.uuid4()), UUID_SUBTYPE) prints | |
# {'my_uuid': Binary('aef72f5c-0f6f-4c77-93be-13601966606f', 3), '_id': ObjectId('4dafa2199f8356069e000000')} | |
# {'my_uuid': Binary('fe22b1e2-9bb4-4fe1-9b5e-3d116f825b34', 3)} | |
# > db.uuids.find(); | |
# { "_id" : ObjectId("4dafa2199f8356069e000000"), "my_uuid" : BinData(3,"YWVmNzJmNWMtMGY2Zi00Yzc3LTkzYmUtMTM2MDE5NjY2MDZm") } | |
# { "_id" : ObjectId("4dafa2199f8356069e000001"), "my_uuid" : BinData(3,"ZmUyMmIxZTItOWJiNC00ZmUxLTliNWUtM2QxMTZmODI1YjM0") } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment