[Answered ]-Pickup "unique item" of a field in a django model using python

2👍

Here’s your dataset:

from itertools import groupby

money = money_tx.objects.order_by('-tx_date')
plans = investment_plan.objects.select_related()
result = []

for date, group in groupby(money, lambda x: x.tx_date):
    plans_for_date = set(x.investment_plan_id for x in group)
    for plan in plans:
        data = {'ORG': plan.organization.organization_name,
                'INV_PLAN': plan.plan_code,
                'DATE': date,
                'PRESENT': (plan.id in plans_for_date)}
        result.append(data)

print result

Leave a comment