开发者

Creating a dynamic query

开发者 https://www.devze.com 2023-04-10 22:31 出处:网络
Please tell me how to make this query dynamic: products = category.product_set.filter(Q(feature__value__value=\'Red\')

Please tell me how to make this query dynamic:

products = category.product_set.filter(Q(feature__value__value='Red')
|Q(feature__value__value='Blue'), feature__name__name='Color')

I need to show all the开发者_高级运维 products with the color red and blue.

And tell me please how best to do this query:

All products with a red and blue, but with Option-1. I understand that right?:

products = category.product_set.filter(Q(feature__value__value='Red')
|Q(feature__value__value='Blue'), feature__name__name='Color').filter(Q(feature__value__value='Yes')
|Q(feature__value__value='No', feature__name__name='Option-1')


I think what you're looking for is this:

# create a dynamic set of keywords
kwargs = {}
name = 'Color'
# since you're looking for literals, I would simply use 'in' as the query
kwargs[name + "__in"] = ['Red','Blue']

products = category.product_set.filter(**kwargs)
0

精彩评论

暂无评论...
验证码 换一张
取 消