我想将以下查询转换为 django 查询模型。
我们有一个products
包含 subcategory_id 的表。通过此查询,我们将能够为每个类别获得 3 种产品。请帮助我们将此 mysql 查询转换为 Django 查询模型。
ID : title : subcategory_id
1 : A : 4
2: B : 6
3: C : 7
4: D : 4
5: E : 4
6 : G : 4
7: F : 6
SELECT * FROM (
SELECT
`products`.*,
@rn := CASE WHEN @subcategory_id=subcategory_id THEN @rn + 1 ELSE 1 END AS rn,
@subcategory_id := `subcategory_id`
FROM `products`, (SELECT @rn := 0, @subcategory_id := NULL) AS vars
ORDER BY `subcategory_id`
) AS T1
WHERE rn <= 3