Django Annotate resolve different value than bucle for в одном подзапросе
У меня есть такой запрос с аннотацией:
whateverquery=Whatever.objects.filter(query,important_field__isnull=False).values('important_field','id').annotate(
total=Count('id'),
total_contract_by_user=Count(
Subquery(OtherWhatever.objects.filter(
whatever__important_field=OuterRef('important_field'),
status="asignada",
visibility=True,
confirm=True).values('user').distinct(),output_field=IntegerField()))
).values('total','total_contract'
).annotate(tasa_conversion=Cast(F('total_contract_by_user')/F('total'),FloatField())
).values('total','total_contract','tasa_conversion'
).order_by(order)
Не решает то же значение в total_contract_by_user, что:
for w in whateverquery:
total_contract_by_user = OtherWhatever.objects.filter(visibility=True,confirm=True,status="asignada",Whatever__important_field=w['important_field']).values('user').distinct()
tasa_conversion = round((total_contract_by_user.count() / int(w['total']))*100,2)
Я не знаю, что я сделал неправильно в Annotate в Query.