from django import template register = template.Library() @register.filter def attr_for_each(iterable, attrname): """Iterate through the supplied iterable and return the `attrname` eattribute of each element in the iterable. Useful with the `join` filter.""" return [getattr(elem, attrname) for elem in iterable]