def dict_to_filter_params(d, prefix=""):
    """
    Flattens a dictionary of attributes to a set of parameters suitable for filtering
    a `QuerySet` that uses `__` as separator.
    """
    params = {}

    for key, val in d.items():
        k = prefix + key
        if isinstance(val, dict):
            params.update(dict_to_filter_params(val, k + "__"))
        else:
            params[k] = val

    return params