Pyramid is a method to distribute the contents of the models.py file to multiple files

  • 2020-04-02 13:15:47
  • OfStack

We changed models.py to a package using the following file structure.


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

Meta. Py, above, defines an additional model file sharing Base and DBSession in the code.


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

Foo.py and mo.py are the specific model definitions
Foo.py and moo.py use the base defined in meta-.py

To ensure that the defined models can be enabled in the program, introduce them in models/ s/ s. Py:


from .meta import DBSession
from .foo import Foo
from .moo import Moo


Related articles: