简体   繁体   English

如何按 Django 中另一个表的记录计数对查询集进行排序

[英]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现在我想获取产品查询集并按客户订单历史记录中购买次数最多的商店进行排序

Product Model产品 Model

id ID product_name产品名称 store_id store_id store_name商店名称 price价格 ..... ......
1 1 iPhone 14 iPhone 14 1 1 Amazon亚马逊 100 100 ..... ......
2 2 iPhone 14 iPhone 14 2 2 Shopee虾皮 1 1 ..... ......
3 3 iPhone 12 iPhone 12 3 3 Taobao淘宝 100 100 ..... ......
4 4 iPhone 13 iPhone 13 1 1 Amazon亚马逊 80 80 ..... ......
5 5 iPhone 14 iPhone 14 3 3 Taobao淘宝 100 100 ..... ......

Order Model订购 Model

id ID product_id product_id customer_id客户ID customer_name顾客姓名
1 1 1 1 1 1 Mike麦克风
2 2 2 2 1 1 Mike麦克风
3 3 4 4 1 1 Mike麦克风
4 4 1 1 2 2 Jhon
5 5 3 3 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 ID product_name产品名称 store_id store_id store_name商店名称 price价格 ..... ......
1 1 iPhone 14 iPhone 14 1 1 Amazon亚马逊 100 100 ..... ......
4 4 iPhone 13 iPhone 13 1 1 Amazon亚马逊 80 80 ..... ......
2 2 iPhone 14 iPhone 14 2 2 Shopee虾皮 1 1 ..... ......
3 3 iPhone 12 iPhone 12 3 3 Taobao淘宝 100 100 ..... ......
5 5 iPhone 14 iPhone 14 3 3 Taobao淘宝 100 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 ID product_name产品名称 store_id store_id store_name商店名称 price价格 ..... ......
3 3 iPhone 12 iPhone 12 3 3 Taobao淘宝 100 100 ..... ......
5 5 iPhone 14 iPhone 14 3 3 Taobao淘宝 100 100 ..... ......
1 1 iPhone 14 iPhone 14 1 1 Amazon亚马逊 100 100 ..... ......
2 2 iPhone 14 iPhone 14 2 2 Shopee虾皮 1 1 ..... ......
4 4 iPhone 13 iPhone 13 1 1 Amazon亚马逊 80 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM