Changes between Version 2 and Version 3 of Ticket #18375, comment 22


Ignore:
Timestamp:
May 30, 2013, 7:46:22 AM (11 years ago)
Author:
German M. Bravo

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #18375, comment 22

    v2 v3  
    11I would definitely go for an `annotate()`, for the akaariai's example:
    2 {{{
     2{{{#!python
    33People.objects.filter(friends__age__gt=F('friends__age') * 2)
    44}}}
     
    66
    77If one, however, wants the friends with 2x the age (two separate joins), it'd need `annotate()` , like so:
    8 {{{
     8{{{#!python
    99People.objects.annotate(twice_age=F('friends__age') * 2).filter(friends__age__gt=F('twice_age'))
    1010}}}
    1111
    1212How 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:
    13 {{{
     13{{{#!python
    1414People.objects.filter(friends__age__gt=F('twice_age')).annotate(twice_age=F('friends__age') * 2)
    1515}}}
Back to Top