本地评估 Django 链接查询集

发布于 2024-08-27 09:07:15 字数 575 浏览 4 评论 0原文

我希望有人能帮我解决一个关于链接 Django 查询集的简单问题。我注意到速度变慢,因为我正在评估数据库中的许多数据点以创建数据趋势。我想知道是否有一种方法可以在本地评估链接的过滤器而不是访问数据库。这是一个(粗略的)示例:

pastries = Bakery.objects.filter(productType='pastry') # <--- will obviously always hit DB, when evaluated
cannoli = pastries.filter(specificType='cannoli') # <--- can this be evaluated locally instead of hitting the DB when evaluated, as long as pastries was evaluated?

我已经检查了文档,但没有看到任何指定这一点的内容,所以我想这是不可能的,但我想首先检查“智囊”;-)。

顺便说一句 - 我知道我可以通过实现一些方法来循环这些数据点并评估标准来自己完成此操作,但是数据点太多,我的截止日期不允许我手动实现此操作。

提前致谢。

I am hoping someone can help me out with a quick question I have regarding chaining Django querysets. I am noticing a slow down because I am evaluating many data points in the database to create data trends. I was wondering if there was a way to have the chained filters evaluated locally instead of hitting the database. Here is a (crude) example:

pastries = Bakery.objects.filter(productType='pastry') # <--- will obviously always hit DB, when evaluated
cannoli = pastries.filter(specificType='cannoli') # <--- can this be evaluated locally instead of hitting the DB when evaluated, as long as pastries was evaluated?

I have checked the docs and I do not see anything specifying this, so I guess it's not possible, but I wanted to check with the 'braintrust' first ;-).

BTW - I know that I can do this myself by implementing some methods to loop through these datapoints and evaluate the criteria, but there are so many datapoints that my deadline does not permit me manually implementing this.

Thanks in advance.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒废 2024-09-03 09:07:15

QuerySet 方法始终生成返回所需表达式的 SQL。这就是为什么你不能在切片后调用各种方法; SQL 不支持该语法。 ORM 只不过是组装所说的 SQL。如果您想要更高级的处理,那么您需要自己在 Python 代码中执行它。

QuerySet methods always produce SQL that returns the desired expression. This is why you cannot e.g. call various methods after slicing; SQL does not support that syntax. The ORM does nothing more than assemble said SQL. If you want fancier processing then you will need to perform it in Python code yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文