Fork me on GitHub

One sometimes does not want to filter on a condition, but use a condition as a Boolean field.

What problems are solved with this?

One can use Q objects to filter a condition. But sometimes you also want to use it in an annotation. For example to add an extra attribute to the model objects, or to order for example.

What does this pattern look like?

One can wrap the Q object in a ExpressionWrapper and specify the BooleanField as output_field=…, for example:

from django.db.models import BooleanField, ExpressionWrapper, Q