认为:
class ItemToBill(models.Model):
date_to_bill = fields.DateField()
description = fields.charfield()
customerToBill = fields.ForeignKey(Customer)
我想找到今天之前应该计费的所有项目,然后按客户对它们进行分组,这样我就可以为每个需要它的客户创建一张发票。
for unique_customer in all_unique_customers_with_items_to_bill:
createInvoice(unique_customer, their_items_to_bill)
我可能会做一些事情来查询商品(由客户订购),然后确定我何时输入了新客户的一组商品。这看起来像:
items = ItemToBill.objects.filter(date_to_bill=BEFORE_TODAY).order_by(customer)
prevCustomer = items[0].customer
customer_items = []
for item in items:
if prevCustomer != item.customer:
createInvoice(prevCustomer, customer_items)
customer_items = []
prevCustomer = item.customer
customer_items.append(item)
createInvioce(prevCustomer, customer_items) #Handle the last customer
但必须有一个更聪明的解决方案。建议?