Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
2153 Vistas

I created 2 fields inside the stock.move.line inside transfers : The first one is many2one field and I need to populate it automatically if the second customized boolean field is checked. The field I need to populate with is the product_id which is already found in the lines. Any Ideas? Here is my code: 


@api.onchange('product_id')
def relate_alternatives(self):
for rec in self:
if rec.alternative_delivery_item==True:
rec.product_alternative=rec.product_id


I don't need an onchange function, I need when checking the box, I need the new many2one field to be filled automatically, at the same time I need when changing the product_id again not affect the other many2one field I created. Thanks


Avatar
Descartar
Mejor respuesta

Hello Moe,

If you don't want an onchange you can compute that field keeping the boolean field('alternative_delivery_item') as the dependent field.

Here is the way of doing that.

product_alternative = fields.Many2one(compute="_compute_product_alternative")

@api.depends('alternative_delivery_item')
def _compute_product_alternative(self):
for rec in self:
if not rec.product_alternative and rec.alternative_delivery_item:
rec.product_alternative=rec.product_id

The field 'product_alternative', once filled will not be affected even if product_id is changed.

Note: If you want your M2O field('product_alternative') to be empty when the boolean is unchecked,use the below code.

@api.depends('alternative_delivery_item')
def _compute_product_alternative(self):
for rec in self:
if not rec.product_alternative and rec.alternative_delivery_item:
rec.product_alternative=rec.product_id
elif not rec.alternative_delivery_item:
rec.product_alternative=False

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari


Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
2
feb 25
5828
1
dic 24
1403
1
feb 23
2433
1
nov 22
15921
3
ago 22
12907