3

我正在尝试_users使用couchdb-python. 我是这里的初学者couchdb

我用 couchdb Document 映射了 python 类用户,couchdb.mapping.Document如下所示:

import couchdb.mapping as cmap


class User(cmap.Document):
    name = cmap.TextField()
    password = cmap.TextField()
    type = 'user'
    roles = {}

但这不起作用。我很doc.type must be user ServerError可能是我声明类型不正确的方式。

我应该如何构建要与_users数据库一起使用的类?

4

1 回答 1

2

在 IRC 频道的一些提示之后,#couchdb我完成了这门课(这可能比我要求的要多......)

import couchdb.mapping as cmap

class User(cmap.Document):
    """  Class used to map a user document inside the '_users' database to a
    Python object.

    For better understanding check https://wiki.apache.org
        /couchdb/Security_Features_Overview

    Args:
        name: Name of the user
        password: password of the user in plain text
        type: (Must be) 'user'
        roles: Roles for the users

    """

    def __init__(self, **values):
        # For user in the _users database id must be org.couchdb.user:<name>
        # Here we're auto-generating it.
        if 'name' in values:
            _id = 'org.couchdb.user:{}'.format(values['name'])
            cmap.Document.__init__(self, id=_id, **values)

    type = cmap.TextField(default='user')
    name = cmap.TextField()
    password = cmap.TextField()
    roles = cmap.ListField(cmap.TextField())

    @cmap.ViewField.define('users')
    def default(doc):
        if doc['name']:
            yield doc['name'], doc

这应该有效:

db = couchdb.server()['_users']
alice = User(name="Alice", password="strongpassword")
alice.store(db)
于 2016-03-29T14:12:25.460 回答