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
.
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.
Can anyone give me an example of a pyramid project structure which has models in different files?
now
meta.py
can contain a sharedBase
as well as theDBSession
:Each
foo.py
andmoo.py
can import their shared base frommeta.py
.To ensure that all of your tables are picked up, from within the
models
subpackage, and for convenience, you can import them intomodels/__init__.py
:Without doing something like this the different tables will not be attached to the
Base
and thus will not be created whencreate_all
is invoked.Your
initialize_db
script can then create all of the tables viaYour views can import the models to profit:
I had the same problem once.
The solving for the splited model files: you must initialize all Base (parent) classes from your files separately: