from typing import OrderedDict


def _translate_reverse_relationships(opts, reverse_relations):
    """
    DRF's `_get_reverse_relationships` uses the `get_accessor_name` method of
    `ForeignObjectRel` as the key for the relationship. This function replaces
    those keys with the rel's `name` property. This allows us to later lookup
    the relationship with `opts.get_field()`.
    """
    return OrderedDict([(
        relation.name, reverse_relations.pop(relation.get_accessor_name())
    ) for relation in opts.related_objects])