我正在使用couchdbkit
构建一个小型 Flask 应用程序,并且我正在尝试编写一些 Python 模型,以便与 DB 交互更容易(而不是内联)。
到目前为止,这是我的代码:
基础.py
from couchdbkit import *
from api.config import settings
class WorkflowsCloudant(Server):
def __init__(self):
uri = "https://{public_key}:{private_key}@{db_uri}".format(
public_key=settings.COUCH_PUBLIC_KEY,
private_key=settings.COUCH_PRIVATE_KEY,
db_uri=settings.COUCH_DB_BASE_URL
)
super(self.__class__, self).__init__(uri)
class Base(Document):
def __init__(self):
server = WorkflowsCloudant.get_db(settings.COUCH_DB_NAME)
self.set_db(server)
super(self.__class__, self).__init__()
工作流.py
from couchdbkit import *
from api.models.base import Base
class Workflow(Base):
workflow = DictProperty()
account_id = IntegerProperty()
created_at = DateTimeProperty()
updated_at = DateTimeProperty()
deleted_at = DateTimeProperty()
status = StringProperty()
控制器 初始化.py
from api.models import Workflow
blueprint = Blueprint('workflows', __name__, url_prefix='/<int:account_id>/workflows')
@blueprint.route('/<workflow_id>')
def get_single_workflow(account_id, workflow_id):
doc = Workflow.get(workflow_id)
if doc['account_id'] != account_id:
return error_helpers.forbidden('Invalid account')
return Response(json.dumps(doc), mimetype='application/json')
我不断收到的错误是:TypeError: doc database required to save document
我试图遵循此处的设置(http://couchdbkit.org/docs/gettingstarted.html),但将它们的内联指令外推到更多的动态上下文中。另外,我是Python新手,所以我为我的无知道歉