@register.filter
def sort_by(items, attr):
    """
    General sort filter - sorts by either attribute or key.
    """
    def key_func(item):
        try:
            return getattr(item, attr)
        except AttributeError:
            try:
                return item[attr]
            except __HOLE__:
                getattr(item, attr)  # Reraise AttributeError
    return sorted(items, key=key_func)
dataset/ETHPy150Open stephenmcd/mezzanine/mezzanine/core/templatetags/mezzanine_tags.py/sort_by