【问题标题】:How to create views with cloudant如何使用 cloudant 创建视图
【发布时间】:2019-05-08 10:25:04
【问题描述】:

在文档https://python-cloudant.readthedocs.io/en/latest/database.html 中显示如何创建文档和数据库,但没有显示如何创建视图

有人可以帮助我吗? 我正在将 cloudant 与 python(Flask) 一起使用...

class TestContext(unittest.TestCase):
def setUp(self):
    self.client_couchdb = CouchDB(
        user='admin',
        auth_token='token123',
        url='https://couchbk.123',
        connect=True
    )

    self.doc_test = {
        '_id': 'julia102',
        'name': 'Julia',
        'age': 30,
        'type': 'event'
    }

    self.db = self.client_couchdb.create_database('test')
    self.db.create_document(self.doc_test)

【问题讨论】:

标签: python flask couchdb python-cloudant


【解决方案1】:

感谢亚历克西斯https://stackoverflow.com/users/5236185/alexis-c%C3%B4t%C3%A9

这是正确的解决方案:

https://python-cloudant.readthedocs.io/en/latest/design_document.html#cloudant.design_document.DesignDocument.add_view

这就是肮脏的解决方案

    self.new_view = {
        '_id': '_design/myname',
        '_rev': 'rev-code',
        'views': {
            'by_client': {
                'map': '''function (doc) {\nif(
                    doc.type === "myname" && doc.client_id
                ){\nemit(doc.client_id);\n}\n}
            '''
            }
        },
        'language': 'javascript'
    }

    # create new view like a doc
    self.db.create_document(self.new_view)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 2021-04-02
    • 1970-01-01
    相关资源
    最近更新 更多