user977828 user977828 - 1 year ago 77
Python Question

couchdb-python and map function ViewField execute

How is it possible to execute in couchdb-python a map function which is in ViewField define

>>> from couchdb import Server
>>> from couchdb.mapping import Document, TextField, IntegerField, DateTimeField, ViewField
>>> db = server.create('python-tests')

>>> class Person(Document):
... _id = TextField()
... name = TextField()
... age = IntegerField()
... by_name = ViewField('people', '''\
... function(doc) {
... emit(, doc);
... }''')
>>> person = Person(_id='Person1', name='John Doe', age=42)
<Person u'Person1'@u'1-95aa43bc1639f0602812ef78deca0a96' {'age': 42, 'name': u'John Doe'}>
>>> Person.by_name(db)
<ViewResults <PermanentView '_design/people/_view/by_name'> {}>

>>> for row in db.query(Person.by_name(db)):
... print row.key
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/mit/apps/pymodules/lib/python2.7/site-packages/CouchDB-0.9-py2.7.egg/couchdb/", line 713, in query
File "/home/mit/apps/pymodules/lib/python2.7/site-packages/CouchDB-0.9-py2.7.egg/couchdb/", line 1041, in __init__
self.map_fun = dedent(map_fun.lstrip('\n\r'))
AttributeError: 'ViewResults' object has no attribute 'lstrip'

Answer Source

You can call query method with map function as first argument:

for row in db.query(Person.by_name.map_fun):
    print row.key

Look for query method signature

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download