Created
October 6, 2015 11:18
-
-
Save lu911/24ef7a702ad08afccc3d to your computer and use it in GitHub Desktop.
database.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
class LoupQuery(Query): | |
pass | |
db_session = scoped_session( | |
sessionmaker( | |
autocommit=False, | |
autoflush=False, | |
expire_on_commit=False, | |
bind=db_engine, | |
query_cls=LoupQuery | |
) | |
) | |
class LoupBase(object): | |
@declared_attr | |
def query(cls): | |
query_cls = getattr(cls, 'query_cls', LoupQuery) | |
return db_session.query_property(query_cls=query_cls) | |
Base = declarative_base(cls=LoupBase) | |
class Item(Base): | |
name = Column(Unicode(30), nullable=False) | |
price = Column(Float, nullable=False) | |
class ItemQuery(LoupQuery): | |
def filter_by_name(self, name): | |
return self.filter(Item.name == name) | |
def filter_by_price(self, price): | |
return self.filter(Item.price == price) | |
query_cls = ItemQuery | |
>>> Item.query.filter_by_name(u'test').filter_by_price(100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment