简体   繁体   English

sqlalchemy 中的.filter() 和.where() 之间的区别

[英]Difference between .filter() and .where() in sqlalchemy

I've seen a few variations of running a query with SQLAlchemy.我已经看到了一些使用 SQLAlchemy 运行查询的变体。 For example, here is one version:例如,这是一个版本:

posts = db.query(models.Post).filter(models.Post.owner_id==user.id).all()

What would be the difference between using the above or using .where ?使用上述或使用.where有什么区别? Why are there two variations here?为什么这里有两种变体?

According to the documentation , there is no difference.根据文档,没有区别。

method sqlalchemy.orm.Query.where(*criterion)方法sqlalchemy.orm.Query.where(*criterion)

A synonym for Query.filter() . Query.filter()的同义词。

It was added in version 1.4 by this commit .它是由这个提交在 1.4 版中添加的。 According to the commit message the reason to add it was to Convert remaining ORM APIs to support 2.0 style .根据提交消息,添加它的原因是为了将剩余的 ORM API 转换为支持 2.0 样式

You can read more about "2.0 style" here .您可以 在此处阅读有关“2.0 风格”的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM