query.get和query.filter\u by(id=id)之间有什么区别?何时使用filter\u by?

2024-04-26 18:29:00 发布

您现在位置:Python中文网/ 问答频道 /正文

documentation中指出

get() is special in that it provides direct access to the identity map of the owning Session.

这在许多方面类似于query.filter_by(id == id)query.filter(ModelClass.id == id)(我假设ModelClass.id是它的主键)

在我看来,我总是希望使用query.get,因为它更快(因为不必执行SELECT)。我为什么要用filter_by(id=id)


Tags: theinidgetbythatisdocumentation
1条回答
网友
1楼 · 发布于 2024-04-26 18:29:00

.get确实执行SELECT当需要时,没有办法。但是,是的,.get足够聪明,可以在可能的情况下绕过DB查询

如果您只按id过滤,那么就没有理由使用filter——这只是更一般的事情

相关问题 更多 >