Hi,
I have problems getting a manipulator right.
This is the object I want to store:
class Model(object):
"""Data Object representing a Model in the databse"""
def __init__(self, name, shortname, price=None, vendor=None, guid=None):
if guid == None:
self.guid = uuid.uuid4()
self.name = name
self.shortname = shortname
if price == None:
self.price = decimal.Decimal(0)
else:
self.price = decimal.Decimal(price)
self.vendor = vendor
self.created = datetime.utcnow()
self.last_modified = datetime.utcnow()
Ok that won't work, since I have a custom object:
>>> from assetkit.model.model import Model
>>> from pymongo import Connection
>>> connection = Connection()
>>> connection.drop_database("custom_type_example")
>>> db = connection.custom_type_example
>>> c = db.model_collection
>>> m = Model(name="Model Name", shortname="MN")
>>> c.insert(m)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/martin/Projects/python-assetkit/lib/python2.5/site-packages/pymongo/collection.py",
line 242, in insert
docs = [self.__database._fix_incoming(doc, self) for doc in docs]
File "/home/martin/Projects/python-assetkit/lib/python2.5/site-packages/pymongo/database.py",
line 212, in _fix_incoming
son = manipulator.transform_incoming(son, collection)
File "/home/martin/Projects/python-assetkit/lib/python2.5/site-packages/pymongo/son_manipulator.py",
line 73, in transform_incoming
son["_id"] = ObjectId()
TypeError: 'unicode' object does not support item assignment
>>>
Let's create a transformer and try again:
class Transformer(SONManipulator):
def transform_incoming(self, son, collection):
"To my understanding son should be the instance of the model
class I want to save, right?"
print "SON %r" % (son, )
for elem in son:
print "ELEM: %r" % (elem, )
key = elem
value = son[elem]
if isinstance(value, Model):
son[key] = value
elif isinstance(value, dict):
pass
return son
def transform_outgoing(self, son, collection):
pass # yes this cannot work...
>>> from pymongo import Connection
>>> connection = Connection()
>>> connection.drop_database("custom_type_example")
>>> db = connection.custom_type_example
>>> db.add_son_manipulator(Transformer())
>>> c = db.model_collection
>>> m = Model(name="Model Name", shortname="MN")
>>> c.insert(m)
SON u'name'
ELEM: u'n'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/martin/Projects/python-assetkit/lib/python2.5/site-packages/pymongo/collection.py",
line 242, in insert
docs = [self.__database._fix_incoming(doc, self) for doc in docs]
File "/home/martin/Projects/python-assetkit/lib/python2.5/site-packages/pymongo/database.py",
line 212, in _fix_incoming
son = manipulator.transform_incoming(son, collection)
File "<stdin>", line 9, in transform_incoming
TypeError: string indices must be integers
obiously the SONs in my transformer are the names of the attributes in
my custom class...I have no idea how to proceed, I have a feeling I'm
on the completely wrong track here :)
Finally some version info:
$ mongo # running Debian/Squeeze
MongoDB shell version: 1.2.3
url: test
connecting to: test
type "help" for help
> db.version()
1.4.0
>
any help?
thanks,
Martin
--
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher
You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.
Please avoid sending me Word or PowerPoint attachments.
See
http://www.gnu.org/philosophy/no-word-attachments.html
--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to
mongod...@googlegroups.com.
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/mongodb-user?hl=en.