How to order a table_query

Hello,

I want to order a o2m field that points to the next table_query class which join stock_move, stock_location and stock_lot tables:

    @classmethod
    def table_query(cls):
        pool = Pool()
        Move = pool.get('stock.move')
        Location = pool.get('stock.location')
        Lot = pool.get('stock.lot')
        table = Move.__table__()
        lot = Lot.__table__()
        location = Location.__table__()
        group_by = []
        for name in ['company', 'product', 'shipment', 'production_input',
                'production_output', 'from_location', 'to_location', 'state',
                'description']:
            group_by.append(Column(table, name))
        group_by.append(Column(location, 'name'))
        columns = [] + group_by
        columns.append(Max(table.id).as_('id'))
        columns.append(Max(table.create_date).as_('create_date'))
        columns.append(Max(table.create_uid).as_('create_uid'))
        columns.append(Max(table.write_uid).as_('write_uid'))
        columns.append(Max(table.write_date).as_('write_date'))
        columns.append(Sum(table.internal_quantity).as_('quantity'))
        columns.append(StringAgg(Coalesce(lot.number, ''), ',').as_('lots'))
        return table.join(lot, type_='LEFT',
            condition=(lot.id == table.lot)).join(
                location, type_='LEFT', condition=(location.id == table.from_location)).select(*columns,
                group_by=group_by, order_by=(location.name))

I expect the o2m to be order by from_location name and I tried different queries but it never returns me the moves ordered. I am missing something?

Thanks in advice.

There is no point to define an order on the table query. The ORM will apply the order defined as usual. So for a One2Many, it is the order defined on the field or the default order defined on the target Model.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.