Home > Backend Development > Python Tutorial > Pyramid将models.py文件的内容分布到多个文件的方法

Pyramid将models.py文件的内容分布到多个文件的方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-16 08:46:04
Original
1528 people have browsed it

我们通过下面的文件结构,将models.py改成一个package.

复制代码 代码如下:

myapp
    __init__.py
    scripts
        __init__.py
        initialize_db.py
    models
        __init__.py
        meta.py
        foo.py
        moo.py

上面的 meta.py, 在代码中定义一个其它model文件共享Base和DBSession.

复制代码 代码如下:

Base = declarative_base()
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension))

foo.py和moo.py就是具体的model定义
foo.py 和moo.py 使用meta.py中定义的base

为了保证定义的model能够在程序中被启用,在models/__init__.py中引入它们:

复制代码 代码如下:

from .meta import DBSession
from .foo import Foo
from .moo import Moo
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template