我的views.py里有这个
# Get the amount of kilo attached to products
product_data = {}
for product in ProductSpy.objects.all():
product_data[product.id] = {"product_id": product.product_id, "kilo": product.kilo}
# Get quantity bought of each product
total_qty_bought = self.order_item_model.objects.values("product").annotate(Sum("quantity"))所以在我的product_data转储文件中,我得到了这个::
6: {'product_id': 32, 'kilo': Decimal('25.00')},在我的total_qty_bought转储中,我得到了这个:
{'product': 32, 'quantity__sum': Decimal('5')},现在,当product_id与product匹配时,我希望它返回125,因为我们得到的5是25千克的数量的倍数。在我的total_qty_bought转储文件中,您会发现id是不相关的,所以如果它们与我的product_data转储文件中的任何一个都不匹配,我希望忽略它们。
实现这一目标的最聪明的方法是什么?
打印转储:
product_data:
{4: {'product_id': 30, 'kilo': Decimal('25.00')}, 5: {'product_id': 31, 'kilo': Decimal('25.00')}, 6: {'product_id': 32, 'kilo': Decimal('25.00')}, 7: {'product_id': 33, 'kilo': Decimal('25.00')}, 8: {'product_id': 37, 'kilo': Decimal('15.00')}, 9: {'product_id': 38, 'kilo': Decimal('15.00')}, 10: {'product_id': 39, 'kilo': Decimal('10.00')}, 11: {'product_id': 40, 'kilo': Decimal('10.00')}, 12: {'product_id': 41, 'kilo': Decimal('5.00')}, 13: {'product_id': 42, 'kilo': Decimal('2.50')}, 14: {'product_id': 43, 'kilo': Decimal('5.00')}, 15: {'product_id': 44, 'kilo': Decimal('2.50')}, 17: {'product_id': 50, 'kilo': Decimal('2.50')}, 18: {'product_id': 51, 'kilo': Decimal('5.00')}, 19: {'product_id': 52, 'kilo': Decimal('10.00')}, 20: {'product_id': 53, 'kilo': Decimal('15.00')}, 21: {'product_id': 55, 'kilo': Decimal('15.00')}, 22: {'product_id': 56, 'kilo': Decimal('10.00')}, 23: {'product_id': 57, 'kilo': Decimal('5.00')}, 24: {'product_id': 58, 'kilo': Decimal('2.50')}}
total_qty_bought:
<QuerySet [{'product': 42, 'quantity__sum': Decimal('5')}, {'product': 41, 'quantity__sum': Decimal('3')}, {'product': 51, 'quantity__sum': Decimal('13')}, {'product': 50, 'quantity__sum': Decimal('34')}, {'product': 49, 'quantity__sum': Decimal('1')}, {'product': 40, 'quantity__sum': Decimal('2')}, {'product': 43, 'quantity__sum': Decimal('10')}, {'product': 52, 'quantity__sum': Decimal('12')}, {'product': 32, 'quantity__sum': Decimal('5')}, {'product': 53, 'quantity__sum': Decimal('2')}, {'product': 38, 'quantity__sum': Decimal('1')}, {'product': 55, 'quantity__sum': Decimal('1')}, {'product': 56, 'quantity__sum': Decimal('10')}, {'product': 58, 'quantity__sum': Decimal('60')}, {'product': 44, 'quantity__sum': Decimal('16')}, {'product': 57, 'quantity__sum': Decimal('28')}]>发布于 2021-02-23 07:51:42
product_data = {}
for product in ProductSpy.objects.all():
product_data[product.product_id] = product.kilo
# Get quantity bought of each product
total_qty_bought = self.order_item_model.objects.values("product").annotate(Sum("quantity"))
our_total = {}
for qty in total_qty_bought:
product_id = qty['product']
quantity = int(qty["quantity__sum"])
try:
kilos = quantity * product_data[product_id]
our_total[product_id] = kilos
print(f"Product_id: {product_id} - Kilos: {kilos}")
except KeyError:
print(f"Product_id {product_id} not found in product_data!")这就是解决方案。
https://stackoverflow.com/questions/66320016
复制相似问题