gpt4 book ai didi

python - 从 SQLAlchemy 中的自定义查询类查找正在查询的类?

转载 作者:行者123 更新时间:2023-11-28 22:01:58 25 4
gpt4 key购买 nike

我目前正在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com