PonyORM - multiple model files

那年仲夏 提交于 2019-12-03 16:45:22

You can use the following project structure:

# /myproject
#     settings.py
#     main.py
#     /models
#         __init__.py
#         base.py
#         requisitions.py
#         workorders.py
#         sales_orders.py

settings.py is a file with database settings:

# settings.py
db_params = {'provider': 'sqlite', 'filename': ':memory:'}

main.py is a file when you start application. You put db.generate_mapping here:

# main.py
from pony import orm
import settings
from models import db
from your_favorite_web_framework import App

# orm.set_sql_degug(True)
db.bind(**settings.db_params)
db.generate_mapping(create_tables=True)

with orm.db_session:
    orm.select(u for u in db.User).show()

if __name__ == '__main__':
    app = App()
    app.run()

Note that it is not necessary to implicitly import all models, as they are accessible as attributes of db object (like db.User)

You can put db object in base.py (or general.py), where you define your core models:

# base.py
from pony import orm

db = orm.Database()

class User(db.Entity):
    name = Required(str)
    orders = Set('Order')

Note that in User model I can refer to Order model defined in another module. You can also write it as

    orders = Set(lambda: db.Order)

Unfortunately, IDEs like PyCharm at this moment cannot recognize that db.Order refers to specific Order class. You can import and use Order class directly, but in some cases it will lead to problem with cyclic imports.

In other model files you import db from .base:

# workorders.py
from pony import orm
from .base import db

class Order(db.Entity):
    description = Optional(str)
    user = Required('User')

In /models/__init__.py you import all modules to ensure that all models classes are defined before generate_mapping is called:

# /models/__init__.py
from .base import db
from . import workorders
...

And then you can write

from models import db

And access models as db attributes like db.User, db.Order, etc.

If you want to refer models directly in your code instead of accessing them as db attributes, you can import all models in __init__.py:

# /models/__init__.py
from .base import db, User
from .workorders import Order
...

And then you can import model classes as:

from models import db, User, Order, ...
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!