1

我想在与数据库建立连接之前删除我的模型(出于某种原因,例如多线程和数据库配置 uri 的动态加载)。

文档说要这样使用:

from ming import create_datastore
from ming.odm import ThreadLocalODMSession
from ming import schema
from ming.odm import FieldProperty
from ming.odm.declarative import MappedClass

session = ThreadLocalODMSession(
    bind=create_datastore('odm_welcome')
)

class WikiPage(MappedClass):
    class __mongometa__:
        session = session
        name = 'wiki_page'

    _id = FieldProperty(schema.ObjectId)
    title = FieldProperty(schema.String(required=True))
    text = FieldProperty(schema.String(if_missing=''))

我们可以看到模型声明需要什么session(在 中__mongometa__)。如何声明没有session变量的 WikiPage 模型?然后再设置?

4

1 回答 1

1

解决方案可以在没有以下情况下声明模型__mongometa__

class WikiPage(MappedClass):
    _id = FieldProperty(schema.ObjectId)
    title = FieldProperty(schema.String(required=True))
    text = FieldProperty(schema.String(if_missing=''))

然后使用集合手动进行映射:

session = ODMSession(bind=create_datastore(uri))
collection_ = collection('wiki_page', session)
session.mapper(WikiPage, collection_)
于 2017-01-24T08:33:11.657 回答