Skip to content

Instantly share code, notes, and snippets.

@JorgeMichelena
Created September 19, 2022 18:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JorgeMichelena/b76c1a0baa30a526113f10b7b0189e0a to your computer and use it in GitHub Desktop.
Save JorgeMichelena/b76c1a0baa30a526113f10b7b0189e0a to your computer and use it in GitHub Desktop.
targets = Target.objects.annotate(
    has_matches=Case(
        When(
            Q(matches_1__isnull=False) | Q(matches_2__isnull=False),
            then=Value(True)
        ),
        default=Value(False),
        output_field=models.BooleanField(),
    )
).prefetch_related("matches_1", "matches_2").order_by("-has_matches")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment