BDu*_*elz 2 python polymorphism sqlalchemy
我现在正在SQLA中做这样的事情:
class Base(object):
    query = DBSession.query_property()
    @classmethod
    def _get_fulltext_query(cls, terms):
        # Search is a method which runs a fulltext search in Whoosh for objects of the given type, returning all ids which match the given terms
        ids = search(terms, cls.__name__)
        return cls.query.filter(cls.id.in_(ids))
我想设置一个自定义查询类,并执行以下操作:
class BaseQuery(Query):
    def fulltext(self, terms):
        # Need a way to find out what class we're querying so that I can run the fulltext search and return the proper query
class Base(object):
    query = DBSession.query_property(BaseQuery)
它对我来说似乎更干净.我还有其他用例需要知道正在查询的类 - 例如,一系列Notification类,我需要知道要返回的通知类型.
有没有办法找出BaseQuery.fulltext中正在查询的类?
这应该工作:
class BaseQuery(Query):
    def fulltext(self, terms):
        # assuming query is always created with `cls.query` or `DBSession.query(cls)`
        cls = self._entities[0].type
        ...
| 归档时间: | 
 | 
| 查看次数: | 198 次 | 
| 最近记录: |