'Add possibly missing JOIN to ``query`` and generate the ORDER BY clause for m2o fields,
either native m2o fields or function/related fields that are stored, including
intermediate JOINs for inheritance if required.
:return: the qualified field name to use in an ORDER BY clause to sort by ``order_field``'

@api.model
def _generate_m2o_order_by(self, alias, order_field, query, reverse_direction, seen):

    field = self._fields[order_field]
    if field.inherited:
        qualified_field = self._inherits_join_calc(alias, order_field, query)
        (alias, order_field) = qualified_field.replace('"', '').split('.', 1)
        field = field.base_field
    assert (field.type == 'many2one'), 'Invalid    field    passed    to    _generate_m2o_order_by()'
    if (not field.store):
        _logger.debug('Many2one    function/related    fields    must    be    stored    to    be    used    as    ordering    fields!    Ignoring    sorting    for    %s.%s', self._name, order_field)
        return []
    dest_model = self.env[field.comodel_name]
    m2o_order = dest_model._order
    if (not regex_order.match(m2o_order)):
        m2o_order = dest_model._rec_name
    join = (alias, dest_model._table, order_field, 'id', order_field)
    (dest_alias, _) = query.add_join(join, implicit=False, outer=True)
    return dest_model._generate_order_by_inner(dest_alias, m2o_order, query, reverse_direction, seen)
