2

我目前在 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 中查询的类?

4

1 回答 1

5

这应该有效:

class BaseQuery(Query):
    def fulltext(self, terms):
        # assuming query is always created with `cls.query` or `DBSession.query(cls)`
        cls = self._entities[0].type
        ...
于 2012-09-03T23:02:20.777 回答