def sort_sequence_by_key(sequence, key_name, reverse=False):
    """
    often when setting up initial serializations (especially during testing),
    I pass a list of dictionaries representing a QS to some fn.
    That list may or may not be sorted according to the underlying model's "order" attribute
    This fn sorts the list according to the value of "key" in each list item;
    typically, "key" would match the "order" attribute of the model
    :param key_name: name of key to sort by
    :param list: list to sort
    :return:
    """
    def _sorting_fn(item):
        # using this fn ensures that 'sort_sequence_by_key' will work
        # for a list of dictionaries or a list of objects
        # (the latter is a special use-case; a QS can use the '.order_by' filter, but an actual list of models cannot)
        try:
            return item.get(key_name)
        except AttributeError:
            return getattr(item, key_name)

    sorted_sequence = sorted(
        sequence,
        key=lambda item: _sorting_fn(item),
        reverse=reverse,
    )
    return sorted_sequence