Help

0

Api onchange: How to make a many2one field dependent of a selection field value?

Avatar
Jan Krähling

Using Odoo 13 Community Edition on Windows.


I'm trying to learn how onchange works.

I want to change a many2one field (id) when the user chooses a specefic value from a selection field.


I have the selection field:

vorgang_auswahl = fields.Selection([

        ('gewinde', 'Gewinde schneiden'),

        ('senken', 'Senken'),

        ('kanten', 'Kanten'),

        ('mikro', 'Mikroecken entfernen'),

        ('schweissen', 'Schweißen'),

        ('schleifen', 'Schleifen')],

        string="Operations", default="gewinde")



So if the user choose the value "gewinde" the field "workcenter_id" should change to a specefic workcenter.


I'm doing this in the "mrp_routing.py".


I tried to get an answer from other questions but still i dont get it.


The base is something like:


    @api.onchange('vorgang_auswahl')

    def _onchange_vorgang_auswahl(self):

        if (((self.vorgang auswahl = 'gewinde')))

        self.workcenter_id = ((('6')))

 

Avatar
Discard
1 Answer
0
Avatar
Niyas Raphy
Best Answer

Hi,

In the onchange of the selection field you can write the value to the Many2one field like this using ID, see this,


@api.onchange('vorgang_auswahl')
def _onchange_vorgang_auswahl(self):
if self.vorgang_auswahl == 'gewinde':
self.workcenter_id = 1


Here I have hard codded and set the value as 1, what this will do is that it will take the record with ID 1 from the co model of the many2one field and assign to this field.


OnChange Function in Odoo: https://www.youtube.com/watch?v=qyRhjyp1MeE


Thanks

Avatar
Discard