1

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'
user977828
  • 7,259
  • 16
  • 66
  • 117

2 Answers2

1

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

apopovych
  • 175
  • 1
  • 7
1

don't forget to store the design_doc make with the ViewField mapping. To do this call sync() :

Person.by_name.sync(db)

and after you will be able to use the view like this :

for person in Person.by_name(db):
     print person._id

I don't have any reference in the package's documentation. Look at the docstring of ViewDefinition Class in python shell or at the code source https://github.com/oliora/couchdb-python/blob/master/couchdb/design.py

  • you are right, just curious can you please point me to documentation for .sync method. I spent an hour on this, but glad this worked out – Romaan Jan 19 '16 at 06:03
  • sorry I've just seen your question at the begin of the month and I didn't have the rights to add any comments until today but I've already change my answer. So I don't have any reference in the package's documentation. I found some information in ViewDefinition's docstrings. – Matthieu Guionnet Sep 28 '16 at 13:10