python - Django - Optimizing displaying each group of rows that sums to a specific amount -


i looking optimal solution following problem:

let's have group of people, each spending different amounts of money on different transactions. transactions stored in following table:

class transactions(models.model):      amount = decimalfield(max_digits=12, decimal_places=2)      created_on = datefield(auto_now_add=true)      t_person = foreignkey('person') 

given specific date, find each person spends on amount in 30 days following date. more specifically, return list of each transaction user made, grouped user, along total amount spent. here first attempt:

def get_transaction(amount, month_start, month_end):     total_transactions = transaction.objects.filter(created_on__gte=month_start, created_on__lte=month_end)     distinct_people = total_transactions.distinct('t_person').values('t_person')     amounts = []     query = []      p in distinct_people:         transactions = total_transactions.filter(t_person=p['t_person'])         p_sum = transactions.aggregate(sum('amount'))         if p_sum['amount__sum'] >= float(amount):             amounts.append(p_sum['amount__sum'])             query.append(transactions)      return zip(amounts, query) 

this works fine now, fear inefficient way accomplish can done simpler. there easier way done?


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -