Skip to content
Advertisement

how to iterate over one field in jinja2

I have a database that looks like this:

JavaScript

and I want to group the rows on country and display with something like this:

JavaScript

The above doesn’t seem to work as the first for loop results in the following error:

JavaScript

Is it possible to extract just one column, and use that to group the rows, just by using jinja2 (i.e. no additional massaging of the database using python) ?

Advertisement

Answer

The error you are seeing is caused by selectattr as it filters and returns a list of dictionaries, and dictionaries are not hashable types. In order to fix this, you can use the map filter to extract the country field from each dictionary and return a list of strings instead:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement