Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
2456 Widoki

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






Awatar
Odrzuć
Najlepsza odpowiedź

It seems you're facing difficulties in updating the "product_qty" field in your manufacturing order based on the "unidades" field. To achieve this without overriding the field, you can use a computed field to calculate the value of "product_qty" based on the "unidades" and "pastones_por_unidad" fields. Here's an updated version of your code:


from odoo import fields, models, api

class MrpProduction(models.Model):
_inherit = 'mrp.production'

unidades = fields.Integer(string='Unidades', compute='_compute_unidades', inverse='_inverse_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
product_qty = fields.Float(string='Quantity', compute='_compute_product_qty', inverse='_inverse_product_qty')

@api.depends('product_qty', 'pastones_por_unidad')
def _compute_unidades(self):
for production in self:
if production.pastones_por_unidad != 0:
production.unidades = production.product_qty // production.pastones_por_unidad
else:
production.unidades = 0

@api.depends('unidades', 'pastones_por_unidad')
def _compute_product_qty(self):
for production in self:
production.product_qty = production.unidades * production.pastones_por_unidad

def _inverse_unidades(self):
for production in self:
production.product_qty = production.unidades * production.pastones_por_unidad

def _inverse_product_qty(self):
for production in self:
if production.pastones_por_unidad != 0:
production.unidades = production.product_qty // production.pastones_por_unidad
else:
production.unidades = 0

In this code, I've made the following changes:

  1. Added a computed field product_qty to replace the original field. This field will now be computed based on the unidades and pastones_por_unidad fields.
  2. Added a new compute method _compute_product_qty that calculates the value of product_qty based on the unidades and pastones_por_unidad fields.
  3. Added an inverse method _inverse_product_qty to update the unidades field when product_qty is changed.
  4. Added an inverse method _inverse_unidades to update the product_qty field when unidades is changed.



Awatar
Odrzuć
Autor

Hello, I've already tried this and did not work. Also, this is not what I want.

I need to do this in a way that I don't override the existing product_qty field, or else the whole mrp module will start malfunctioning

Powiązane posty Odpowiedzi Widoki Czynność
1
cze 23
2607
1
sty 22
2574
1
maj 20
4128
0
kwi 24
1383
1
cze 23
2730