SQLAlchemy classes across files

前端 未结 4 1560
渐次进展
渐次进展 2020-12-02 05:55

I\'m trying to figure out how to have SQLAlchemy classes spread across several files, and I can for my life not figure out how to do it. I am pretty new to SQLAlchemy so for

4条回答
  •  一向
    一向 (楼主)
    2020-12-02 06:30

    I'm using Python 2.7 + Flask 0.10 + SQLAlchemy 1.0.8 + Postgres 9.4.4.1

    This boilerplate comes configured with a User and UserDetail models stored in the same file "models.py" in the "user" module. These classes both inherit from an SQLAlchemy base class.

    All of the additional classes I've added to my project also derived from this base class, and as the models.py file grew larger, I decided to split the models.py file into one file per class, and ran into the problem described here.

    The solution I found, along the same lines as @computermacgyver's Oct 23 2013 post, was to include all my classes to the init.py file of the new module I created to hold all the newly created class files. Looks like this:

    /project/models/
    
    __init__.py contains
    
    from project.models.a import A 
    from project.models.b import B
    etc...
    

提交回复
热议问题