基于我选择的多个过滤器,我在JSON下面创建了。
{
"category1": ["A", "B", "C"],
"category2": ["x", "y", "z"],
"category3": ["med1", "med2"]
}
我正在寻找从JSON以上动态创建的以下查询
query=((Q(category__name__contains="category1") &
(Q(subcategory__name__contains="A")|
Q(subcategory__name__contains="B")|Q(subcategory__name__contains="C"))
)|(Q(category__name__contains="category2") &
(Q(subcategory__name__contains="x")|
Q(subcategory__name__contains="y")|Q(subcategory__name__contains="z"))
)|(Q(category__name__contains="categor3") &
(Q(subcategory__name__contains="med1")|
Q(subcategory__name__contains="med2"))
))
Product.objects.filter(查询)
关于如何做到这一点的任何想法?我是否必须使用行SQL查询或者可以通过Django查询来完成?
答案 0 :(得分:0)
用于:
query = (Q(category__name__contains="category1") &
Q(subcategory__name__in=json['category1'])) |
(Q(category__name__contains="category2") &
Q(subcategory__name__in=json['category2'])) |
(Q(category__name__contains="categor3") &
Q(subcategory__name__in=json['categor3']))