开发者

Evaluating Django Chained QuerySets Locally

开发者 https://www.devze.com 2022-12-24 08:16 出处:网络
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 t

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 开发者_JAVA技巧'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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号