odoo 如何传递context到name_get 方法

发布时间 2023-09-07 12:15:09作者: 那时一个人

xml 需要按照如下写法: 看move_id字段,要点是要在关键字前边加一个default_, 这个本来是用来创建时指定默认值的,目前就这么用吧

                        <tree editable="top" create="0">
                            <field name="product_id" options="{'no_create': True, 'no_open': True}" force_save="1"/>
                            <field name="move_id" context="{'default_move_uom':1}"/>
                            <field name="quantity"/>
                            <field name="uom_id" readonly="1" groups="uom.group_uom"/>
                        </tree>

在py文件中获取到的context

    def name_get(self):
        res = []
        print('name_get:{}'.format(self._context))
        if self._context.get('move_uom'):
            for move in self:
                res.append((move.id, '%s%s%s>%s' % (
                    move.picking_id.origin and '%s/' % move.picking_id.origin or '',
                    '{}({})'.format(move.name,move.hs_package),
                    move.location_id.name, move.location_dest_id.name)))
            return res

# name_get:{'lang': 'en_US', 'tz': 'Asia/Shanghai', 'uid': 232, 'move_uom': 1}