使用CouchDB-Kit和Python;尝试在不设置DB-inlin的情况下设置数据库

2024-06-01 08:18:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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在

^{pr2}$

控制器 初始化.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新手,所以我很抱歉我的无知


Tags: keypyselfiddbdocsettingsinit
1条回答
网友
1楼 · 发布于 2024-06-01 08:18:24

如果模型(文档)没有链接到数据库(正确),则会发生此错误。这是通过使用set_db方法完成的。在

我也认为你应该改变你的模式:

from couchdbkit import Document
from couchdbkit import StringProperty, IntegerProperty
from couchdbkit import DateTimeProperty,  DictProperty

class Workflow(Document):
    workflow = DictProperty()
    account_id = IntegerProperty()
    created_at = DateTimeProperty()
    updated_at = DateTimeProperty()
    deleted_at = DateTimeProperty()
    status = StringProperty()

我将基继承改为Document类。还要避免使用from some_module import *!在

当您设置了这样的模型之后,您可以像这样链接模型和couchdb:

^{pr2}$

注意:代码未经测试。我是从头开始写的,所以可能会有一些错误。在

相关问题 更多 >