couchdb python和map函数ViewField execute
如何在couchdb python中执行ViewField define中的映射函数couchdb python和map函数ViewField execute,python,couchdb,couchdb-python,Python,Couchdb,Couchdb Python,如何在couchdb python中执行ViewField define中的映射函数 >>> from couchdb import Server >>> from couchdb.mapping import Document, TextField, IntegerField, DateTimeField, ViewField >>> db = server.create('python-tests') >>> clas
>>> 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'
来自couchdb导入服务器的>>
>>>从couchdb.mapping导入文档,文本字段,整数字段,日期时间字段,视图字段
>>>db=server.create('python-tests')
>>>班级人员(文件):
... _id=TextField()
... name=TextField()
... 年龄=整数字段()
... 按名称=视野('people','''
…函数(doc){
…发出(doc.name,doc);
... }''')
...
>>>person=person(_id='Person1',name='John Doe',年龄=42岁)
>>>个人存储(db)
>>>人员姓名(db)
>>>对于db.query(Person.by_name(db))中的行:
... 打印row.key
...
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/home/mit/apps/pymodules/lib/python2.7/site packages/CouchDB-0.9-py2.7.egg/CouchDB/client.py”,第713行,在查询中
包装器=包装器)(**选项)
文件“/home/mit/apps/pymodules/lib/python2.7/site packages/CouchDB-0.9-py2.7.egg/CouchDB/client.py”,第1041行,在__
self.map\u fun=dedent(map\u fun.lstrip('\n\r'))
AttributeError:“ViewResults”对象没有属性“lstrip”
您可以使用map函数作为第一个参数调用查询方法:
for row in db.query(Person.by_name.map_fun):
print row.key
查找方法签名别忘了用视场映射存储设计文档。 要执行此调用sync(): 之后,您将能够像这样使用视图:
for person in Person.by_name(db):
print person._id
我在包的文档中没有任何参考资料。
查看python shell中ViewDefinition类的docstring或代码源你是对的,只是好奇你能给我指一下.sync方法的文档吗。我花了一个小时讨论这个问题,但很高兴这个问题解决了。我在本月初刚刚看到你的问题,直到今天我才有权添加任何评论,但我已经更改了我的答案。所以我在包的文档中没有任何参考资料。我在ViewDefinition的文档字符串中找到了一些信息。
for person in Person.by_name(db):
print person._id