[英]How to sort queryset by another table's record count in Django
I have a Product table and order table the Product table record product info,and the Order table record customer's purchases records我有一个产品表和订单表,产品表记录产品信息,订单表记录客户的购买记录
Now I want to get the products queryset and sort by the store with the most purchases in a customer's order history现在我想获取产品查询集并按客户订单历史记录中购买次数最多的商店进行排序
id ![]() |
product_name![]() |
store_id ![]() |
store_name![]() |
price![]() |
..... ![]() |
---|---|---|---|---|---|
1 ![]() |
iPhone 14 ![]() |
1 ![]() |
Amazon![]() |
100 ![]() |
..... ![]() |
2 ![]() |
iPhone 14 ![]() |
2 ![]() |
Shopee![]() |
1 ![]() |
..... ![]() |
3 ![]() |
iPhone 12 ![]() |
3 ![]() |
Taobao![]() |
100 ![]() |
..... ![]() |
4 ![]() |
iPhone 13 ![]() |
1 ![]() |
Amazon![]() |
80 ![]() |
..... ![]() |
5 ![]() |
iPhone 14 ![]() |
3 ![]() |
Taobao![]() |
100 ![]() |
..... ![]() |
id ![]() |
product_id ![]() |
customer_id![]() |
customer_name![]() |
---|---|---|---|
1 ![]() |
1 ![]() |
1 ![]() |
Mike![]() |
2 ![]() |
2 ![]() |
1 ![]() |
Mike![]() |
3 ![]() |
4 ![]() |
1 ![]() |
Mike![]() |
4 ![]() |
1 ![]() |
2 ![]() |
Jhon![]() |
5 ![]() |
3 ![]() |
3 ![]() |
Simon![]() |
in my case,I want to get the product's queryset and sort by the store with the most purchases in a customer's order history就我而言,我想获取产品的查询集并按客户订单历史记录中购买次数最多的商店进行排序
For example:例如:
when customer_id is 1(Mike),the product queryset should be like below because Mike have spent the most times on Amazon, so the ordering of products should put Amazon's products first当customer_id为1(Mike)时,商品查询集应该如下,因为Mike在亚马逊上花费的时间最多,所以商品的下单应该把亚马逊的商品放在第一位
id ![]() |
product_name![]() |
store_id ![]() |
store_name![]() |
price![]() |
..... ![]() |
---|---|---|---|---|---|
1 ![]() |
iPhone 14 ![]() |
1 ![]() |
Amazon![]() |
100 ![]() |
..... ![]() |
4 ![]() |
iPhone 13 ![]() |
1 ![]() |
Amazon![]() |
80 ![]() |
..... ![]() |
2 ![]() |
iPhone 14 ![]() |
2 ![]() |
Shopee![]() |
1 ![]() |
..... ![]() |
3 ![]() |
iPhone 12 ![]() |
3 ![]() |
Taobao![]() |
100 ![]() |
..... ![]() |
5 ![]() |
iPhone 14 ![]() |
3 ![]() |
Taobao![]() |
100 ![]() |
..... ![]() |
In the same case,when customer_id is 3(Simon),the product queryset should be like below,because Mike have spent the most times on Taobao同样的情况下,当customer_id为3(Simon)时,产品查询集应该如下,因为Mike在淘宝上花费的时间最多
id ![]() |
product_name![]() |
store_id ![]() |
store_name![]() |
price![]() |
..... ![]() |
---|---|---|---|---|---|
3 ![]() |
iPhone 12 ![]() |
3 ![]() |
Taobao![]() |
100 ![]() |
..... ![]() |
5 ![]() |
iPhone 14 ![]() |
3 ![]() |
Taobao![]() |
100 ![]() |
..... ![]() |
1 ![]() |
iPhone 14 ![]() |
1 ![]() |
Amazon![]() |
100 ![]() |
..... ![]() |
2 ![]() |
iPhone 14 ![]() |
2 ![]() |
Shopee![]() |
1 ![]() |
..... ![]() |
4 ![]() |
iPhone 13 ![]() |
1 ![]() |
Amazon![]() |
80 ![]() |
..... ![]() |
I user count and filter in Django,code below, but it executes the result is wrong customer_id = 1 # Mike我在 Django 中进行用户计数和过滤,代码如下,但执行结果错误 customer_id = 1 # Mike
product_set = Product.objects.annotate(count=Count('order__store_id', filter=Q(order__customer_id=customer_id, order__store__id=F('store_id')))).order_by('-count')
I think you what you want is, if apple is a product and store 1 sold it 15 times store 2 sold it 25 times我想你想要的是,如果苹果是一种产品,商店 1 卖了 15 次,商店 2 卖了 25 次
then product apple with store id 1 comes on top然后商店 id 为 1 的产品 apple 排在首位
so for this:所以为此:
from django.db.models import Count
Product.objects.annotate(store_count=Count('store_id')).order_by('-store_count')
or或者
queryset.objects.annotate(store_count=Count('store_id')).order_by('-store_count')
this will create a temporary field store_count
and Count('store_id')
will count product occurrences with store_id and then order it.这将创建一个临时字段
store_count
并且Count('store_id')
将使用 store_id 计算产品出现次数,然后对其进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.