id,summary,reporter,owner,description,type,status,component,version,severity,resolution,keywords,cc,stage,has_patch,needs_docs,needs_tests,needs_better_patch,easy,ui_ux 31606,Cannot mix Exists expression with keyword arguments to When,Ryan Heard,Ryan Heard,"I don't seem to be able to provide an Exists expression to When alongside keyword arguments like you can with filter. For instance, consider: {{{ class State(models.Model): pass class County(models.Model): name = CharField(max_length=50) state = ForeignKey(State, related_name='counties') }}} I can execute the following query just fine: {{{ County.objects.filter( Exists(State.objects.filter(counties=OuterRef('pk'), name=""Texas"")), name=""Dallas"", ) }}} But a similar query using When does not work: {{{ >>> County.objects.annotate( status=Case( When(Exists(State.objects.filter(counties=OuterRef('pk'), name=""Texas"")), name=""Dallas"", then=Value(""DALLAS COUNTY"")), default=Value(""ELSEWHERE""), )) TypeError: When() supports a Q object, a boolean expression, or lookups as a condition. }}} Instead the arguments must be wrapped in a Q object: {{{ >>> County.objects.annotate( status=Case( When(Q(Exists(State.objects.filter(counties=OuterRef('pk'), name=""Texas"")), name=""Dallas""), then=Value(""DALLAS COUNTY"")), default=Value(""ELSEWHERE""), )) }}} This is inconvenient and inconsistent with how filter works, as shown. When's __init__ method can be modified to allow similar input as filter. [https://github.com/rheard/django/commit/978e796bb18414d995d515ab7923831149300977 Code is in a branch in my repo], but as this is my first time contributing to Django, I want to make sure I open a ticket and get feedback first. Also I wasn't sure how to classify this. I wasn't sure if it was a bug, as I wasn't sure if it was designed this way.",New feature,closed,"Database layer (models, ORM)",dev,Normal,fixed,,,Ready for checkin,1,0,0,0,1,0