Fix BuildLineFilter.filter_allocated

- Required for mysql backend
This commit is contained in:
Oliver Walters 2026-06-23 04:43:15 +00:00
parent ec845c61cd
commit 7102ea4d6e
1 changed files with 16 additions and 2 deletions

View File

@ -494,9 +494,23 @@ class BuildLineFilter(FilterSet):
def filter_allocated(self, queryset, name, value):
"""Filter by whether each BuildLine is fully allocated."""
allocated_subquery = (
BuildItem.objects
.filter(build_line=OuterRef('pk'))
.values('build_line')
.annotate(total=Sum('quantity'))
.values('total')
)
queryset = queryset.alias(
allocated_quantity=Coalesce(Subquery(allocated_subquery), 0)
)
if str2bool(value):
return queryset.filter(allocated__gte=F('quantity') - F('consumed'))
return queryset.filter(allocated__lt=F('quantity') - F('consumed'))
return queryset.filter(
allocated_quantity__gte=F('quantity') - F('consumed')
)
return queryset.filter(allocated_quantity__lt=F('quantity') - F('consumed'))
consumed = rest_filters.BooleanFilter(label=_('Consumed'), method='filter_consumed')