我有这样的模型:
class Category(Model):
...
type = models.CharField(max_length=255, choices=settings.CATEGORIES)
class Product(models.Model):
...
categories = models.ManyToManyField(Category)
我有用户输入的类别列表(格式是任意的):
c = [<Category: G, type: producer>, <Category: L, type: producer>, <Category: Teen, type: age>, <Category: Man, type: sex>]
现在我想拥有所有类别中的“G”或“L”类型为“producer”、“Teen”类型为“age”和“Man”类型为“sex”的产品。例如,我正在做下一个查询集:
Product.objects.filter(Q(categories=c[0])|Q(categories=c[1]), Q(categories=c[2]), Q(categories=c[3])
有这样的查询:
SELECT "shop_product"."id", "shop_product"."title", "shop_product"."description",
"shop_product"."price", "shop_product"."code", "shop_product"."stored_count",
"shop_product"."sold_count", "shop_product"."added_date" FROM "shop_product"
INNER JOIN "shop_product_categories" ON ("shop_product"."id" =
"shop_product_categories"."product_id") WHERE
(("shop_product_categories"."category_id" = 1 OR
"shop_product_categories"."category_id" = 2) AND
"shop_product_categories"."category_id" = 4 AND
"shop_product_categories"."category_id" = 6)
我至少有一个符合此类要求的对象,但 queryset 什么也不返回。有什么建议吗?