如何動態增加 database engine?

4 views
Skip to first unread message

SeedSeek

unread,
Jun 19, 2017, 8:28:45 PM6/19/17
to Uliweb
database engine 沒在 settings.ini 裡面設定,想直接在 .py 的程式裡面加載新的 database engine 給 models 用,要如何做比較好?

limodou

unread,
Jun 20, 2017, 9:43:34 AM6/20/17
to uliweb
下面这段代码是 uliweb.contrib.orm/__init__.py的初始化engine的代码,可以参考:

for name, d in settings.get_var('ORM/CONNECTIONS').items():
x = {'connection_string':d.get('CONNECTION', ''),
'debug_log':d.get('DEBUG_LOG', None),
'connection_args':d.get('CONNECTION_ARGS', {}),
'strategy':d.get('STRATEGY', 'threadlocal'),
'connection_type':d.get('CONNECTION_TYPE', 'long'),
'duplication':d.get('DUPLICATION', False),
}
orm.engine_manager.add(name, x)

主要就是通过 orm.engine_manager.add(name, options)
来添加一个新的engine,然后在Model上使用use(engine_name)来切换。

On Tue, Jun 20, 2017 at 8:28 AM, SeedSeek <frt5...@gmail.com> wrote:
> database engine 沒在 settings.ini 裡面設定,想直接在 .py 的程式裡面加載新的 database engine 給
> models 用,要如何做比較好?
>
> --
> -- ----
> Project : https://github.com/limodou/uliweb
> doc : http://limodou.github.com/uliweb-doc
> ---
> You received this message because you are subscribed to the Google Groups
> "Uliweb" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to uliweb+un...@googlegroups.com.
> To post to this group, send email to uli...@googlegroups.com.
> Visit this group at https://groups.google.com/group/uliweb.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/uliweb/5501ba9c-36af-4349-adf2-a19f132a9d47%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: https://github.com/limodou/uliweb
My Blog: http://my.oschina.net/limodou
Reply all
Reply to author
Forward
0 new messages