我试图使用_users从数据库couchdb-python中存储和检索用户。我是couchdb的初学者。
我用couchdb文档couchdb.mapping.Document映射了python类用户,如下所示:
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数据库一起使用的类?
发布于 2016-03-29 14:12:25
在从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)https://stackoverflow.com/questions/35941497
复制相似问题