2๐
โ
You need a list of items by customer, which sounds like a simple loop.
items_by_customer = {}
for item in ItemToBill.objects.filter(...date_query...):
items_by_customer.setdefault(item.customerToBill, []).append(item)
for customer, items in items_by_customer.items():
print customer, items # items grouped by customer.
# generate_invoice(customer, items)
0๐
I would probably first aggregate all records for each customer into a dictionary:
from collections import defaultdict
items = ItemToBill.objects.filter(date_to_bill=BEFORE_TODAY).order_by(customer)
bills_by_customer = defaultdict(list)
for bill in items:
bills_by_customer[bill.customer.pk].append(bill)
Then you can iterate over the grouped dictionary:
for customer_pk, bills in bills_by_customer.iteritems():
createInvoice(customer_pk, bills)
๐คjterrace
Source:stackexchange.com