This question has been flagged
1 Reply
7359 Views

It is real to change lines in one2many field via on_change function?

For example, I want to set location_id and location_dest_id in move_lines, when user changes this values in stock_picking form.

class stock_picking(osv.osv):
    _name = "stock.picking"
    _inherit = "stock.picking"

    _columns = {
        'location_id': fields.many2one('stock.location', 'Source Location', select=True,states={'done': [('readonly', True)]}),
        'location_dest_id': fields.many2one('stock.location', 'Destination Location', states={'done': [('readonly', True)]}),
        'move_lines': fields.one2many('stock.move', 'picking_id', 'Internal Moves', states={'done': [('readonly', True)], 'cancel': [('readonly', True)]}, ondelete='CASCADE'),
    }

    def update_move_lines(self, cr, uid, ids, location_id, location_dest_id, move_lines, context=None):
        returnValue = {}
        if move_lines:
            for row in move_lines:
                if type(row[2]) != dict:
                    row[2] = {}
                if location_id:
                    row[2]['location_id'] = location_id
                if location_dest_id:
                    row[2]['location_dest_id'] = location_dest_id
            returnValue['value'] = {'move_lines': move_lines}
        return returnValue


class stock_move(osv.osv):

    _name = "stock.move"
    _inherit = "stock.move"

    _columns = {
        'name': fields.char('Description', required=True, select=True),
        'location_id': fields.many2one('stock.location', 'Source Location', required=True, select=True,states={'done': [('readonly', True)]}),
        'location_dest_id': fields.many2one('stock.location', 'Destination Location', required=True,states={'done': [('readonly', True)]}),
    }



<record id="view_picking_form" model="ir.ui.view">
    <field name="name">stock.picking.form</field>
    <field name="model">stock.picking</field>
    <field eval="12" name="priority"/>
    <field name="arch" type="xml">
        <form string="Internal Picking List" version="7.0">
        <sheet>
            <group>
                <group>
                    <field name="location_id" on_change="onchange_location(location_id, location_dest_id, move_lines)"/>
                    <field name="location_dest_id" on_change="onchange_location(location_id, location_dest_id, move_lines)"/>
                </group>
            </group>
            <notebook>
                <page string="Products">
                    <field name="move_lines" context="{'form_view_ref':'view_move_picking_form', 'tree_view_ref':'view_move_picking_tree', 'picking_type': 'internal'}"/>
                </page>
            </notebook>
        </sheet>
        </form>
    </field>
</record>
Avatar
Discard
Best Answer

In your example move_lines is one2many field. If so, please see similar ask for how define o2m value.

Avatar
Discard
Author

Thanks, wowas!