(This is a little contrived)
`MyModel.objects.annotate(bob=F("my_duration")).values("bob")`
works fine, then if we add a bad lookup
`MyModel.objects.annotate(bob=F("my_duration")).values("bob__fred")`
we get an error saying "Cannot resolve keyword 'bob' into field." which is
a misleading error.
--
Ticket URL: <https://code.djangoproject.com/ticket/32848>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* version: 3.2 => 3.1
--
Ticket URL: <https://code.djangoproject.com/ticket/32848#comment:1>
* status: new => closed
* resolution: => needsinfo
Comment:
Hi Gordon. Thanks.
Hmmm. "Cannot resolve keyword 'bob' into field." is exactly what I'm
expecting here. 🤔
I'm going to say `needsinfo`: If you want to suggest a patch implementing
the logic for a better error message here then happy to look at it — but
I'm initially sceptical that backing out of the lookup resolution in order
to check if an annotation was applied is going to be worth the complexity.
However, if something clean is available then it's worth considering.
I hope that makes sense.
--
Ticket URL: <https://code.djangoproject.com/ticket/32848#comment:2>
Comment (by Gordon Wrigley):
If you do it without the annotation, so
`MyModel.objects.values("my_duration__fred")` you "Cannot resolve keyword
'fred' into field." which is what I'd expect. The field "bob" is legit,
it's the lookup 'fred' that is bogus, so that's what I'd expect the error
on.
--
Ticket URL: <https://code.djangoproject.com/ticket/32848#comment:3>
Comment (by João Freires):
I don't think that this ticket is closed. When we try to filter a Queryset
with an annotation, the resolution error is more accurate.
`MyModel.objects.annotate(bob=F("my_duration")).filter('bob__fred')`
Threw an accurate error message: "FieldError: Unsupported lookup
'fred'..."
But with:
`MyModel.objects.annotate(bob=F("my_duration")).values('bob__fred')`
I got: "FieldError: Cannot resolve keyword 'bob' into field."
I didn't try to evaluate how difficult is to do the same resolution in
Queryset's filter and values.
But I think that is good to keep the pattern and the two message errors
give a correct explanation.
--
Ticket URL: <https://code.djangoproject.com/ticket/32848#comment:4>
Comment (by Carlton Gibson):
Very happy to reopen if someone wants to provide a patch showing it's
feasible at a moderate complexity level!
--
Ticket URL: <https://code.djangoproject.com/ticket/32848#comment:5>