from django.db.models import Count from core.apps.finance.models import Expence from django.core.management import BaseCommand def get_duplicate_expenses_all(): """ Barcha counterparty boyicha bir xil summada yaratilgan expenselarni topadi """ expences = Expence.objects.filter( is_deleted=False ).values('counterparty', 'price', 'currency').annotate( count=Count('id') ).filter(count__gt=1).order_by('-count') return expences class Command(BaseCommand): def handle(self, *args, **options): duplicate_data = get_duplicate_expenses_all() result = [] for item in duplicate_data: expenses = Expence.objects.filter( is_deleted=False, counterparty_id=item['counterparty'], price=item['price'], currency=item['currency'] ) counterparty = expenses.first().counterparty if expenses.exists() else None result.append({ 'counterparty': counterparty, 'amount': item['price'], 'currency': item['currency'], 'count': item['count'], 'expenses': expenses }) print(result)