Creating a dynamic query
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)
精彩评论