3 ms·
Given that one says Products.objects.all(), which one do you think it is? Good on you for correctly determining the difference, though!
by sgarland 3y ago
Given that one says Products.objects.all(), which one do you think it is?
Good on you for correctly determining the difference, though!
- happytoexplain 3y agoAh, I see - since I'm not familiar with this language/API, I couldn't really infer what was happening behind the scenes. E.g. '.all()' could be constructing a request, not necessarily executing, and then the 'if' might modify that request intelligently.
- stavros 3y agoYou're right, this is Python/Django, where you don't get these semantics (Product is the database model, and it doesn't have access to the "if" to use in the filter). It was more of a general example, though, of "loop vs filtering in the SELECT", so the language doesn't matter much (well, apart from effectively making my point, which it perhaps failed at).
- Izkata 3y agoIn this ORM, both versions construct without executing until the results are accessed. The list comprehension looping over the results is an access, the "if" happens after that. The one exception to the obvious "results are accessed" intuition is slices (such as [:3] to mean first 3 elements), which thanks to how python implemented them lets the ORM use them to further modify the query by adding LIMIT instead of executing it.