I've got a dictionary that has a structure similar to:
{
'id': 16,
'name': 'something',
'client': 'client1',
},
{
'id': 17,
'name': 'something',
'client': 'client2',
},
{
'id': 18,
'name': 'something-else',
'client': 'client3',
}
I can iterate through my dicts in my templates but I'm trying to get the unique values of name
in this case. I've tried a couple different options such as:
{% for name in data | unique %}
or
{% for name in data.name | unique %}
but none of it returns me a list of unique names.
What's the best way to do this? I've thought about creating a custom filter, but wondering if there is a better way to do it.
From the docs:
Returns a list of unique items from the given iterable.
case_sensitive
– Treat upper and lower case strings as distinct.
attribute
– Filter objects with unique values for this attribute.
Because you want to eliminate duplicates based on the value of the name
argument, you should set the attribute
argument accordingly:
{% for name in data | unique(attribute="name") %}