与sqlalchemy的多租户 - 可以在event.listen上查询查询。

发布于 2025-02-10 08:18:08 字数 469 浏览 1 评论 0原文

我想将Company_ID过滤器添加到所有查询中,并使用SQLalchemy将数据库添加到数据库中。

我已经在下面开火了。

from sqlalchemy import event

def my_load_listener(target, context):
    print("on load!")

event.listen(Settings, 'load', my_load_listener)

看来我可以使用context.query获得一个选择对象,但是我使用以下内容尝试添加过滤器,但不能使其工作。

context.query = context.query.where(Settings.id == 2)

是否有可能拦截模型查询 /插入并随时进行修改?

理想情况下,它可以在全球范围上实现,因此我不必为所有模型添加事件。

I want to add a company_id filter to all queries to the database with sqlalchemy.

I've got the event firing with the below.

from sqlalchemy import event

def my_load_listener(target, context):
    print("on load!")

event.listen(Settings, 'load', my_load_listener)

Looks like I can get a select object with context.query but I've used the below to try and add a filter but cant get it to work.

context.query = context.query.where(Settings.id == 2)

Is it possible to intercept a models query / insert and amend it on the fly?

Ideally it would be achievable on a global scope so I don't have to add events for all models.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文