#18375: F() doesn't work as expected across multijoin relations -------------------------------------+------------------------------------- Reporter: FunkyBob | Owner: akaariai Type: Bug | Status: closed Component: Database layer | Version: 1.4 (models, ORM) | Resolution: fixed Severity: Release blocker | Triage Stage: Accepted Keywords: | Needs documentation: 0 Has patch: 1 | Patch needs improvement: 0 Needs tests: 0 | UI/UX: 0 Easy pickings: 0 | -------------------------------------+------------------------------------- Changes (by Kronuz):
* cc: Kronuz (added) Comment: I would definitely go for an `annotate()`, for the akaariai's example: `People.objects.filter(friends__age__gt=F('friends__age') * 2)` This, after the fix, makes just one join. If one, however, wants the friends with 2x the age (two separate joins), it'd need `annotate()` , like so: `People.objects.annotate(twice_age=F('friends__age') * 2).filter(friends__age__gt=F('twice_age'))` How far is django from such behavior? ...and also, it doesn't still feel entirely good (annotate always creating new joins for such cases), since one might actually want to get the reused join... I would probably suggest the order of the calling of annotate to change things (if putting annotate after the filter, it'd reuse the joins from the previous filter, otherwise it would start afresh). This way, the next query would be equivalent to the first one: `People.objects.filter(friends__age__gt=F('twice_age')).annotate(twice_age=F('friends__age') * 2)` Provided one could use `F()` of non-yet "annotated" things. Any thoughts regarding this? -- Ticket URL: <https://code.djangoproject.com/ticket/18375#comment:22> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/066.83386c7f16523666e4d2a1502da02836%40djangoproject.com?hl=en. For more options, visit https://groups.google.com/groups/opt_out.