12

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?

2 Answers 2

24
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:

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

Each foo.py and moo.py can import their shared base from meta.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:

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.

Your initialize_db script can then create all of the tables via

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
  • Thanks for the answer! I am curious to know why we need the same Base object across all the models. I thought that Base would be some static/shared object which would pick up all the metadata regardless from where it is invoked.
    – Karan
    Apr 27, 2012 at 19:18
  • 1
    You don't need the same base or metadata, but it certainly helps keep things clear. Each database engine should have one metadata object that describes the schema for that engine. In python, modules are only executed when you import them, so when you break the models.py out into multiple individual modules then they won't be picked up until each of them is imported. Apr 27, 2012 at 20:46
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)
4
  • 1
    Not sure it's the best way to go. I'd rather have tried to import Base in each model file. Apr 27, 2012 at 13:36
  • @Antoine:I think that from project.models.Foo import Base as FooBase means there is a Base in each model file. And I am already doing that but it doesnt work. @Vitali: Do you know why do we have to go about doing this in a roundabout way to create separate bases for each model?
    – Karan
    Apr 27, 2012 at 14:42
  • @KaranK: My suggestion was to have a single Base in one file, and import that Base in each model file. I'm not sure the relationships would work if you used different Bases. Apr 27, 2012 at 14:50
  • @Antoine: Michael's answer is similar to what you said. Thanks a lot!
    – Karan
    Apr 27, 2012 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.