perf: account risk filter

pull/14753/head
feng 2025-01-03 11:41:40 +08:00 committed by feng626
parent ac2151bc4b
commit 4fa88aa226
1 changed files with 9 additions and 15 deletions

View File

@ -1,7 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
from django.db.models import Q, F, Value, CharField from django.db.models import Q, Exists, OuterRef
from django.db.models.functions import Concat
from django.utils import timezone from django.utils import timezone
from django_filters import rest_framework as drf_filters from django_filters import rest_framework as drf_filters
@ -63,20 +62,15 @@ class AccountFilterSet(BaseFilterSet):
if not value: if not value:
return queryset return queryset
asset_usernames = AccountRisk.objects.filter(risk=value). \ queryset = queryset.filter(
values_list( Exists(
Concat( AccountRisk.objects.filter(
F('asset_id'), Value('-'), F('username'), risk=value,
output_field=CharField() asset_id=OuterRef('asset_id'),
), flat=True username=OuterRef('username')
) )
queryset = queryset.annotate(
asset_username=Concat(
F('asset_id'), Value('-'), F('username'),
output_field=CharField()
) )
).filter(asset_username__in=asset_usernames) )
return queryset return queryset
@staticmethod @staticmethod