Skip to content

Instantly share code, notes, and snippets.

@lu911
Created October 6, 2015 11:18
Show Gist options
  • Save lu911/24ef7a702ad08afccc3d to your computer and use it in GitHub Desktop.
Save lu911/24ef7a702ad08afccc3d to your computer and use it in GitHub Desktop.
database.py
# -*- 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