我希望在与数据库连接之前删除我的模型(出于某些原因,如多线程和数据库配置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模型?然后再设置吗?
发布于 2017-01-24 16:33:12
解决方案可以在没有__mongometa__情况下声明模型
class WikiPage(MappedClass):
_id = FieldProperty(schema.ObjectId)
title = FieldProperty(schema.String(required=True))
text = FieldProperty(schema.String(if_missing=''))然后手动使用集合进行make映射:
session = ODMSession(bind=create_datastore(uri))
collection_ = collection('wiki_page', session)
session.mapper(WikiPage, collection_)https://stackoverflow.com/questions/41807329
复制相似问题