我目前正在 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)
对我来说,它看起来更干净。我还有其他用例需要知道正在查询什么类 - 例如,一系列通知类,我需要知道要返回什么通知类型。
有什么方法可以找出从 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
...
关于python - 在 SQLAlchemy 中从自定义查询类中查找正在查询的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12253877/