Search code examples
pythoncouchdbcouchdb-python

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.name, doc);
...             }''')
... 
>>> person = Person(_id='Person1', name='John Doe', age=42)                                
>>> person.store(db)                                                                       
<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/client.py", line 713, in query
wrapper=wrapper)(**options)
  File "/home/mit/apps/pymodules/lib/python2.7/site-packages/CouchDB-0.9-py2.7.egg/couchdb/client.py", line 1041, in __init__
self.map_fun = dedent(map_fun.lstrip('\n\r'))
AttributeError: 'ViewResults' object has no attribute 'lstrip'

Solution

  • 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