首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用couchdb-python管理_users中的用户

用couchdb-python管理_users中的用户
EN

Stack Overflow用户
提问于 2016-03-11 13:28:11
回答 1查看 339关注 0票数 3

我试图使用_users从数据库couchdb-python中存储和检索用户。我是couchdb的初学者。

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

代码语言:javascript
复制
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数据库一起使用的类?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-29 14:12:25

在从IRC上的#couchdb频道得到一些提示后,我推出了这个类(这可能比我要求的要多.)

代码语言:javascript
复制
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

这应该是可行的:

代码语言:javascript
复制
db = couchdb.server()['_users']
alice = User(name="Alice", password="strongpassword")
alice.store(db)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35941497

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档