如何使用Pony ORM将db_session中的db_session表创建

时间:2018-01-08 12:38:27

标签: python flask ponyorm

我试图在模型文件夹中分离这两个方面。 我有:pony.orm.core.ERDiagramError:无法定义实体' Guest&#39 ;:数据库映射已经生成

这是代码

******* db_session.py *******

ts = pd.to_datetime(df['Time Stamp'], format='%d/%m/%Y %H:%M:%S:%f').dt.strftime('%Y-%W')

for i, x in df.groupby(ts):
   x.to_csv('{}.csv'.format(i), index=False)

***** dbase.py *****

from pony import orm
from model.dbase import (Guest, Offer, Payment_method, Extra_services,
                     Voucher, Reservation, db)

db = orm.Database()

class GuestManager(object):
    @orm.db_session
    def show_guest(n):
        guest = Guest[n]
        return guest

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

这样的事情可以解决问题。

from pony import orm
from model.dbase import (Guest, Offer, Payment_method, Extra_services,
                         Voucher, Reservation, db)


class GuestManager(object):
    @orm.db_session
    def show_guest(n):
        guest = Guest[n]
        return guest

***** dbase.py *****

from datetime import date
from pony import orm

db = orm.Database()

db.bind(provider='sqlite', filename='database.sqlite')
orm.sql_debug(True)

class Guest(db.Entity):
    nome = orm.Required(str)
    surname = orm.Required(str)
    email = orm.Required(str, unique=True)

db.generate_mapping(create_tables=True)

你不想每次都创建你的表,除非你是从一个新的数据库开始。

在回购

上,

Here是一个很好的问题

答案 1 :(得分:1)

您可以在启动项目中添加数据库初始化,通常是 __ init __。py 文件。

  

__ init __。py

from datetime import date
from pony import orm

db = orm.Database()

def startup_database():
    db.bind(provider='sqlite', filename='database.sqlite', create_db=True)
    db.generate_mapping(create_tables=True)
    orm.sql_debug(True)

def main():
    startup_database() # If you have another methods, THIS LINE SHOULD BE THE LAST ONE

if __name__ == '__main__':
    main()

然后,在其他文件中定义模型:

  

models.py

from pony import orm

# 'db' variable it's included by own because it's on __init__.py file
# You could migrate to another file form __init__.py and import that module here
class Guest(db.Entity):
    nome = orm.Required(str)
    surname = orm.Required(str)
    email = orm.Required(str, unique=True)

并在另一个文件中创建一个经理数据库

  

manager.py

from pony import orm
from models import Guest # Add another models that you have

class GuestManager(object):
    @orm.db_session
    def show_guest(n):
        guest = Guest[n]
        return guest

您可以创建自己喜欢的任何体系结构。希望对您有帮助!