def get_object_from_filter(obj, components):
    """Gets the object whose attributes are actually being queried, which may be
    a different object if there is a chain.

    :param obj: the intial object.
    :param list components: the components of the original key.
    :returns: the relevant object"""
    components = components[:]
    while len(components) > 2:
        obj = getattr(obj, components.pop(0))
    if len(components) == 2:
        if components[-1] != 'regex':
            if not hasattr(obj, f'__{components[-1]}__'):
                obj = getattr(obj, components[0])
    return obj
