Django ORM查询从多个表中获取数据
Django ORM query to get data from multiple tables
我需要从多个table中检索数据来显示店铺名称、店铺位置、店铺月销量(单位)、月销售额以及从店铺接单的销售人员姓名。
我的样本 table 示例如下:
订单
id total_price added_by_id(employee id) customer_id
1 42480 2 1
2 74920 3 2
3 21000 3 2
4 42480 2 1
订单项
order_id unit
1 1
1 2
2 2
2 2
3 2
4 5
客户
id full_name location
1 Shop1 Example location1
2 Shop2 Example location2
员工
id full_name
1 Employee Name1
2 Employee Name2
结果table应该是
shop name location salesperson sales volume monthly sales
Shop1 location1 Employee Name1 8 43630
Shop2 location2 Employee Name2 6 95920
我的尝试:
Order.objects.select_related('account_customer').all()
.values('customer_id')
.annotate(total_sales=Sum('total_price'))
.filter(created_at__year=today.year, created_at__month=today.month)
会return
<QuerySet [{'customer_id': 1, 'total_sales': Decimal('43630.00')}, {'customer_id': 2, 'total_sales': Decimal('95920.00')}]>
我无法检索到其他必要信息,例如店铺名称、位置、销售人员姓名销量。
相关机型:
class Order(models.Model):
added_by = models.ForeignKey(Employee, on_delete=models.CASCADE)
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
total_price = models.DecimalField(max_digits=18, decimal_places=2)
class OrderItem(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE)
unit = models.FloatField()
class Customer(models.Model):
shop_name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
class Employee(AbstractUser):
full_name = models.CharField(max_length=100)
如何检索所有必要的信息?
您可以将必填字段添加到 values()
作为,
Order.objects.select_related('account_customer').all() \
.<b>values('customer_id', 'other_field_1', ...)</b> \
.annotate(total_sales=Sum('total_price')) \
.filter(created_at__year=today.year, created_at__month=today.month)
这相当于 SQL GROUP BY 语句。
有关详细信息,请阅读 official django doc
UPDATE-1
Order.objects.annotate(
location=F('customer__address'),
salesperson=F('added_by'),
shop_name=F('customer__shop_name')
).values('shop_name', 'salesperson', 'location').annotate(sales_volume=Count('orderitem__unit'),
monthly_sales=Count('total_price'))
我需要从多个table中检索数据来显示店铺名称、店铺位置、店铺月销量(单位)、月销售额以及从店铺接单的销售人员姓名。 我的样本 table 示例如下:
订单
id total_price added_by_id(employee id) customer_id
1 42480 2 1
2 74920 3 2
3 21000 3 2
4 42480 2 1
订单项
order_id unit
1 1
1 2
2 2
2 2
3 2
4 5
客户
id full_name location
1 Shop1 Example location1
2 Shop2 Example location2
员工
id full_name
1 Employee Name1
2 Employee Name2
结果table应该是
shop name location salesperson sales volume monthly sales
Shop1 location1 Employee Name1 8 43630
Shop2 location2 Employee Name2 6 95920
我的尝试:
Order.objects.select_related('account_customer').all()
.values('customer_id')
.annotate(total_sales=Sum('total_price'))
.filter(created_at__year=today.year, created_at__month=today.month)
会return
<QuerySet [{'customer_id': 1, 'total_sales': Decimal('43630.00')}, {'customer_id': 2, 'total_sales': Decimal('95920.00')}]>
我无法检索到其他必要信息,例如店铺名称、位置、销售人员姓名销量。
相关机型:
class Order(models.Model):
added_by = models.ForeignKey(Employee, on_delete=models.CASCADE)
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
total_price = models.DecimalField(max_digits=18, decimal_places=2)
class OrderItem(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE)
unit = models.FloatField()
class Customer(models.Model):
shop_name = models.CharField(max_length=100)
address = models.CharField(max_length=100)
class Employee(AbstractUser):
full_name = models.CharField(max_length=100)
如何检索所有必要的信息?
您可以将必填字段添加到 values()
作为,
Order.objects.select_related('account_customer').all() \
.<b>values('customer_id', 'other_field_1', ...)</b> \
.annotate(total_sales=Sum('total_price')) \
.filter(created_at__year=today.year, created_at__month=today.month)
这相当于 SQL GROUP BY 语句。
有关详细信息,请阅读 official django doc
UPDATE-1
Order.objects.annotate(
location=F('customer__address'),
salesperson=F('added_by'),
shop_name=F('customer__shop_name')
).values('shop_name', 'salesperson', 'location').annotate(sales_volume=Count('orderitem__unit'),
monthly_sales=Count('total_price'))