marly
February 18, 2022, 8:26pm
1
I have got dictionary set of required queries as shown below:
[{'primary_attribute__name': 'Color', 'primary_attr_values__name': 'Red'}, {'primary_attribute__name': 'Color', 'primary_attr_values__name': 'Green'}]
Now I want to access/show dictionary element on template or views on following way:
Color : Red
Green
and so on…
How can I do this?
To clarify, are you saying that you want to convert this dictionary:
{'primary_attribute__name': 'Color', 'primary_attr_values__name': 'Red'}
Into a query that looks something like this?:
SomeModel.objects.filter(primary_attribute__name='Color', primary_attr_values__name='Red')
If not, what are you trying to achieve here? It’s not clear what the desired end result is.
marly
February 19, 2022, 6:01am
3
no, Actually it
[{'primary_attribute__name': 'Color', 'primary_attr_values__name': 'Red'}, {'primary_attribute__name': 'Color', 'primary_attr_values__name': 'Green'}]
is the result of
primary_attribute_list = list(ProductAttribute.objects.filter(product__id=product.id).values('primary_attribute__name','primary_attr_values__name'))
Where primary_attribute__name will be same for respective product,but the
primary_attr_values__name
will be different.
On this I want to print like: Color : Red, Green, etc … on template
marly
February 19, 2022, 6:57am
4
Now I want :
[{'primary_attribute__name': 'Color', {
'primary_attr_values__name': 'Red',
'primary_attr_values__name': 'Green',
'primary_attr_values__name': 'Yellow'
}]
That data structure you posted isn’t valid. You’re missing a quote after the first ‘primary_attribute__name’, and the second element isn’t a key:value pair.
[Edit: I see you corrected the quotes issue, but that still isn’t valid syntax.]