与sqlalchemy的多租户 - 可以在event.listen上查询查询。
我想将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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论