Closed DanSheps closed 9 months ago
One-liner if interested:
from django.db.models import Count
Cable.objects.annotate(
aterm=Count('terminations', filter=Q(terminations__cable_end="A")),
bterm=Count('terminations', filter=Q(terminations__cable_end="B")),
).filter(Q(aterm=1, bterm=0) | Q(aterm=0, bterm=1) | Q(aterm=0, bterm=0))
Slight edit, added __gte
:
Cable.objects.annotate(
aterm=Count('terminations', filter=Q(terminations__cable_end="A")),
bterm=Count('terminations', filter=Q(terminations__cable_end="B")),
).filter(Q(aterm__gte=1, bterm=0) | Q(aterm=0, bterm__gte=1) | Q(aterm=0, bterm=0))
Slight edit, added
__gte
:Cable.objects.annotate( aterm=Count('terminations', filter=Q(terminations__cable_end="A")), bterm=Count('terminations', filter=Q(terminations__cable_end="B")), ).filter(Q(aterm__gte=1, bterm=0) | Q(aterm=0, bterm__gte=1) | Q(aterm=0, bterm=0))
Looks like that works, and doesn't have the problems I was coming up against.
Pretty basic, query for orphaned or partially orphaned cables and return the list.