Changes between Version 1 and Version 2 of Ticket #18375, comment 22
- Timestamp:
- May 30, 2013, 7:44:20 AM (11 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #18375, comment 22
v1 v2 1 1 I would definitely go for an `annotate()`, for the akaariai's example: 2 `People.objects.filter(friends__age__gt=F('friends__age') * 2)` 2 {{{ 3 People.objects.filter(friends__age__gt=F('friends__age') * 2) 4 }}} 3 5 This, after the fix, makes just one join. 4 6 5 7 If one, however, wants the friends with 2x the age (two separate joins), it'd need `annotate()` , like so: 6 `People.objects.annotate(twice_age=F('friends__age') * 2).filter(friends__age__gt=F('twice_age'))` 8 {{{ 9 People.objects.annotate(twice_age=F('friends__age') * 2).filter(friends__age__gt=F('twice_age')) 10 }}} 7 11 8 12 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: 9 `People.objects.filter(friends__age__gt=F('twice_age')).annotate(twice_age=F('friends__age') * 2)` 13 {{{ 14 People.objects.filter(friends__age__gt=F('twice_age')).annotate(twice_age=F('friends__age') * 2) 15 }}} 10 16 Provided one could use `F()` of not yet "annotated" things. 11 17