[Answered ]-Django When(…) object SQL missing when used with __in lookup with empty list as value, leading to ProgrammingError

1👍

One idea to avoid this happening is to "switch" the default condition and use Q() instead of ~Q()

.annotate(
    employee_authorized=Case(
        When(Q(case__assigned_unit__in=[]), then=Value(True)),
        default=Value(False),
        output_field=BooleanField(),
    )
)

Leave a comment