Wanting to create a manufacturing order of a product, I want to let the users the choice of selecting the quantity to make in two ways.
On the first hand, they normally introduce the quantity in the existing "product_qty" field, those values referring to the main Unit of measure, per example units.
On the other hand, I also want to let them fill the quantity in another field, those values referring to a secondary Unit of measure, per example boxes, and then get that quantity converted to units and filled in the "product_qty" field. The thing is that depending on the product the units/box change.
For this I made a new field inside product model, and added a related field to that on my mrp.production model... all of this I got it right.
Now the problem is when I want to override the "product_qty" field with an onchange or computed value (preferably computed). I don't know how to apply the computed function to that field without overriding it in my module. i don't want to override it because other functionalities will probably break. I don't know if there is a way to just "extend" the field.
Also, when I try to update it with an onchange it does weird things, like going instantly to "To finish" state just after selecting the product, and even then the field does not get updated.
If I try to do it the other way (convert units to boxes) it works fine, so I'm a little lost with it.
I'll paste the code here:
from odoo import fields, models, api
classMrpProduction(models.Model):
_inherit='mrp.production'
unidades= fields.Integer(string='Unidades',
compute='_compute_unidades', readonly=False) #Field to insert "Boxes"
pastones_por_unidad= fields.Integer(string='Pastones por Unidad', related='product_id.pastones_por_unidad', readonly=True) #Related "Units/Box" field
# This works, to change the number of boxes based on the produc_qty introduced
@api.depends('product_qty', 'pastones_por_unidad')
def_compute_unidades(self):
forproductioninself:
ifproduction.pastones_por_unidad !=0:
production.unidades =production.product_qty //production.pastones_por_unidad
else:
production.unidades =0
# The other way around doesn't, trying to write directly on "product_qty"
@api.onchange('unidades')
def_onchange_product_qty(self):
self.product_qty=self.unidades*self.pastones_por_unidad