Django 嵌套分组按模板中的数据
Django Nested Group By Data in template
这里我解释一下我的模型、数据、table结构和预期结果。
我的模型如下:
class Buyer(models.Model):
name = models.CharField(max_length=100)
code = models.CharField(max_length=20
class Merchand(models.Model):
name = models.CharField(max_length=100)
code = models.CharField(max_length=20
class Order(models.Model):
code = models.CharField(max_length=20)
buyer = models.ForeignKey(CustomUser, on_delete=SET_NULL, related_name='br_order')
merchand = models.ForeignKey(CustomUser, on_delete=SET_NULL, related_name='mr_order')
value = models.FloatField(null=True, blank=True)
qty = models.FloatField(null=True, blank=True)
这是我的模型结构,我的订单模型数据是这样的:
{'code': 'C-001', 'buyer': 1, 'merchand': '1', 'qty': 100, 'value': '100'}
{'code': 'C-002', 'buyer': 1, 'merchand': '1', 'qty': 100, 'value': '300'}
{'code': 'C-003', 'buyer': 2, 'merchand': '2', 'qty': 100, 'value': '400'}
{'code': 'C-004', 'buyer': 3, 'merchand': '2', 'qty': 700, 'value': '400'}
{'code': 'C-005', 'buyer': 2, 'merchand': '2', 'qty': 900, 'value': '4500'}
{'code': 'C-006', 'buyer': 2, 'merchand': '3', 'qty': 200, 'value': '2000'}
{'code': 'C-007', 'buyer': 3, 'merchand': '2', 'qty': 700, 'value': '400'}
{'code': 'C-008', 'buyer': 2, 'merchand': '2', 'qty': 900, 'value': '4500'}
{'code': 'C-009', 'buyer': 2, 'merchand': '3', 'qty': 200, 'value': '2000'}
我想生成这样的 table:
= Buyer: 1, qty: 200, value: 400
- code: C-001, qty: 100, value: 100
- code: C-002, qty: 100, value: 300
= Buyer: 2, qty: 200, value: 13400
- code: C-003, qty: 100, value: 400
- code: C-005, qty: 900, value: 4500
- code: C-006, qty: 200, value: 2000
- code: C-008, qty: 900, value: 4500
- code: C-009, qty: 200, value: 2000
= Buyer: 3, qty: 1400, value: 800
- code: C-004, qty: 700, value: 400
- code: C-007, qty: 700, value: 400
如何显示此类数据?
对于每个标题使用此查询集:
from django.db.models import Sum
result = (Order.objects
.values('buyer')
.annotate(qty=Sum('qty'))
.annotate(value=Sum('value'))
)
对于每个买家 select 买家:
for x in result:
print(x)
buyer_order = Order.objects.filter(buyer = x['buyer'])
for b in buyer_order:
print(f'code = {b.code}, merchand = {b.merchand}, value = {b.value}, qty = {b.qty}')
这里我解释一下我的模型、数据、table结构和预期结果。
我的模型如下:
class Buyer(models.Model):
name = models.CharField(max_length=100)
code = models.CharField(max_length=20
class Merchand(models.Model):
name = models.CharField(max_length=100)
code = models.CharField(max_length=20
class Order(models.Model):
code = models.CharField(max_length=20)
buyer = models.ForeignKey(CustomUser, on_delete=SET_NULL, related_name='br_order')
merchand = models.ForeignKey(CustomUser, on_delete=SET_NULL, related_name='mr_order')
value = models.FloatField(null=True, blank=True)
qty = models.FloatField(null=True, blank=True)
这是我的模型结构,我的订单模型数据是这样的:
{'code': 'C-001', 'buyer': 1, 'merchand': '1', 'qty': 100, 'value': '100'}
{'code': 'C-002', 'buyer': 1, 'merchand': '1', 'qty': 100, 'value': '300'}
{'code': 'C-003', 'buyer': 2, 'merchand': '2', 'qty': 100, 'value': '400'}
{'code': 'C-004', 'buyer': 3, 'merchand': '2', 'qty': 700, 'value': '400'}
{'code': 'C-005', 'buyer': 2, 'merchand': '2', 'qty': 900, 'value': '4500'}
{'code': 'C-006', 'buyer': 2, 'merchand': '3', 'qty': 200, 'value': '2000'}
{'code': 'C-007', 'buyer': 3, 'merchand': '2', 'qty': 700, 'value': '400'}
{'code': 'C-008', 'buyer': 2, 'merchand': '2', 'qty': 900, 'value': '4500'}
{'code': 'C-009', 'buyer': 2, 'merchand': '3', 'qty': 200, 'value': '2000'}
我想生成这样的 table:
= Buyer: 1, qty: 200, value: 400
- code: C-001, qty: 100, value: 100
- code: C-002, qty: 100, value: 300
= Buyer: 2, qty: 200, value: 13400
- code: C-003, qty: 100, value: 400
- code: C-005, qty: 900, value: 4500
- code: C-006, qty: 200, value: 2000
- code: C-008, qty: 900, value: 4500
- code: C-009, qty: 200, value: 2000
= Buyer: 3, qty: 1400, value: 800
- code: C-004, qty: 700, value: 400
- code: C-007, qty: 700, value: 400
如何显示此类数据?
对于每个标题使用此查询集:
from django.db.models import Sum
result = (Order.objects
.values('buyer')
.annotate(qty=Sum('qty'))
.annotate(value=Sum('value'))
)
对于每个买家 select 买家:
for x in result:
print(x)
buyer_order = Order.objects.filter(buyer = x['buyer'])
for b in buyer_order:
print(f'code = {b.code}, merchand = {b.merchand}, value = {b.value}, qty = {b.qty}')