在webapp2中集成sqlalchemy

在webapp2中集成sqlalchemy,sqlalchemy,webapp2,Sqlalchemy,Webapp2,我正在google appengine之外使用webapp2开发一个应用程序,我想集成sqlalchemy并在上找到一个示例,因此我使用了示例中的models.py,并在main.py中定义了BaseHandler,如下所示: class BaseHandler(webapp2.RequestHandler): @property def db(self): """ returns ORM handle to the database

我正在google appengine之外使用webapp2开发一个应用程序,我想集成sqlalchemy并在上找到一个示例,因此我使用了示例中的models.py,并在main.py中定义了BaseHandler,如下所示:

class BaseHandler(webapp2.RequestHandler):
    @property
    def db(self):
        """ 
        returns ORM handle to the database
        Usage: admin = self.db.query(User).filter_by(name='admin').first()
        """
        return scoped_session(sessionmaker(bind=engine))

我的问题是,这是推荐的方法吗?他们是另一条路吗?

我觉得看起来不错。我看到它做得稍有不同,它被抽象出来并被严格用于数据访问的类使用,您的处理程序执行来自这些数据访问类的方法,这可能更清楚。我的想法是,处理程序实际上是用于构造视图的,不需要直接访问数据库。