I am new to pyramid and have been struggling to make some changes to my project. I am trying to split my models/Classes into individual files instead of a single models.py file. In order to do so I have removed the old models.py and created a models folder with __init__.py
file along with one file for each class. In __init__.py
I imported the class by using from .Foo import Foo
.
我是金字塔的新人,一直在努力改变我的项目。我正在尝试将我的模型/类拆分为单个文件,而不是单个的模型。py文件。为了做到这一点,我去掉了旧模型。并创建了一个带有__init__的模型文件夹。py文件和每个类的一个文件。__init__。py我通过使用。Foo导入Foo来导入类。
This makes the views work correctly and they can initialize an object.
这使得视图能够正确地工作,并且可以初始化一个对象。
But running the initializedb script does not create new tables as it did when I had all the models in a single models.py. It does not create the relevant tables but directly tries to insert in them.
但是运行initializedb脚本并不像在单个model .py中那样创建新表。它不创建相关的表,而是直接尝试插入它们。
Can anyone give me an example of a pyramid project structure which has models in different files?
谁能给我举一个金字塔项目结构的例子,它在不同的文件中有模型?
2 个解决方案
#1
20
myapp
__init__.py
scripts
__init__.py
initialize_db.py
models
__init__.py
meta.py
foo.py
moo.py
now meta.py
can contain a shared Base
as well as the DBSession
:
现在元。py可以包含一个共享基,也可以包含DBSession:
Base = declarative_base()
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension))
Each foo.py
and moo.py
can import their shared base from meta.py
.
每个foo。py和牛叫声。py可以从元组.py导入它们的共享基。
from .meta import Base
class Foo(Base):
pass
To ensure that all of your tables are picked up, from within the models
subpackage, and for convenience, you can import them into models/__init__.py
:
为了确保从models子包中获取所有表,并且为了方便起见,您可以将它们导入models/__init__.py:
from .meta import DBSession
from .foo import Foo
from .moo import Moo
Without doing something like this the different tables will not be attached to the Base
and thus will not be created when create_all
is invoked.
如果不这样做,不同的表将不会被附加到基础上,因此在调用create_all时也不会被创建。
Your initialize_db
script can then create all of the tables via
然后,可以通过initialize_db脚本创建所有的表
from myapp.models.meta import Base
Base.metadata.create_all(bind=engine)
Your views can import the models to profit:
您的意见可以导入模型获利:
from myapp.models import DBSession
from myapp.models import Foo
#2
0
I had the same problem once.
我也有过同样的问题。
The solving for the splited model files: you must initialize all Base (parent) classes from your files separately:
分解模型文件的解决:您必须分别从您的文件初始化所有的基础(父类)类:
#initializedb.py
...
from project.models.Foo import Base as FooBase
from project.models.Moo import Base as MooBase
...
def main(argv=sys.argv):
...
FooBase.metadata.create_all(engine)
MooBase.metadata.create_all(engine)
#1
20
myapp
__init__.py
scripts
__init__.py
initialize_db.py
models
__init__.py
meta.py
foo.py
moo.py
now meta.py
can contain a shared Base
as well as the DBSession
:
现在元。py可以包含一个共享基,也可以包含DBSession:
Base = declarative_base()
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension))
Each foo.py
and moo.py
can import their shared base from meta.py
.
每个foo。py和牛叫声。py可以从元组.py导入它们的共享基。
from .meta import Base
class Foo(Base):
pass
To ensure that all of your tables are picked up, from within the models
subpackage, and for convenience, you can import them into models/__init__.py
:
为了确保从models子包中获取所有表,并且为了方便起见,您可以将它们导入models/__init__.py:
from .meta import DBSession
from .foo import Foo
from .moo import Moo
Without doing something like this the different tables will not be attached to the Base
and thus will not be created when create_all
is invoked.
如果不这样做,不同的表将不会被附加到基础上,因此在调用create_all时也不会被创建。
Your initialize_db
script can then create all of the tables via
然后,可以通过initialize_db脚本创建所有的表
from myapp.models.meta import Base
Base.metadata.create_all(bind=engine)
Your views can import the models to profit:
您的意见可以导入模型获利:
from myapp.models import DBSession
from myapp.models import Foo
#2
0
I had the same problem once.
我也有过同样的问题。
The solving for the splited model files: you must initialize all Base (parent) classes from your files separately:
分解模型文件的解决:您必须分别从您的文件初始化所有的基础(父类)类:
#initializedb.py
...
from project.models.Foo import Base as FooBase
from project.models.Moo import Base as MooBase
...
def main(argv=sys.argv):
...
FooBase.metadata.create_all(engine)
MooBase.metadata.create_all(engine)