def collect_parent_models(model, levels=None):
    """Recursively collect a list of parent models."""
    result = []

    if levels == 0:
        return result
    elif levels:
        levels -= 1

    parent_models = model._meta.get_parent_list()

    for parent in parent_models:
        result.append(parent)
        if levels is not None and levels:
            result.extend(collect_parent_models(parent, levels))

    return result