Opened 3 weeks ago
Closed 3 weeks ago
#37059 closed New feature (wontfix)
`.only(..)` does not check eagerly if the fields exists
| Reported by: | Willem Van Onsem | Owned by: | Yujin Kim |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 6.0 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Django's QuerySets are lazy, but the checks for (most) methods are eagerly, I think. For example:
us = User.objects.filter(bla='qux')
will raise an error.
But only(..) seems to be an exception on this. If we use:
us = User.objects.only('bla')
it will only raise if we evaluate us (enumerate, aggregate, str/len/...).
It seems nice that .only(..) would check eagerly if the fields are available, otherwise tests might succeed if the queryset is in some cases not evaluated.
A possible problem might be that .only(..) could strictly speaking mention fields that are not *yet* defined, but later in the chain of ORM method calls.
Change History (2)
comment:1 by , 3 weeks ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:2 by , 3 weeks ago
| Component: | Uncategorized → Database layer (models, ORM) |
|---|---|
| Resolution: | → wontfix |
| Status: | assigned → closed |
Thanks for the suggestion, however, even if feasible, I doubt this is worth the complexity it would likely add, especially considering that
defer()andonly()are only for advanced use cases.