This question has been flagged



class FuelStation_in_stock(models.Model):

     _name= "fuelstation.instock"

    _description ="IN Fuel Record"

    name = fields.Char("Recived By"

    date = fields.Datetime("Date:"

    fuel_type = fields.Many2one(comodel_name ="fuelstation.fueldata",string="Fuel Type")            instock_qut= fields.Float("IN stock Quantity") avl_qut = fields.Float(string="Currently Avilable Quantity",related='fuel_type.avl_qut'

    @api.depends('instock_qut'

    def _update_stock(self): 

        for rec in self

            rec.fuel_type.avl_qut += rec.instock_qut

        self.updated_stock= rec.fuel_type.avl_qut

updated_stock= fields.Float(string="Updated Stock",compute=_update_stock, sto



I have created a compute function with decorator @api.depends

while debugging I checked in inspect view in chrome there I come to know that the onchange function is being called whenever I change the value of 'in_stock'

I have not used @api.onchange anywhere in the module. but the function is being called every time on changing the value of 'instock_qut'.

I have used the same function in another module and it's working as expected.

I'm not getting why is this happening here in this module.

I have also tried using create() and write() methods but still have not gotten the solution. 



Avatar
Discard
Author

Thank you @Niyas

So how can I achieve my goal

to compute the field only once on save

And I used the same code in another module and its works below is the code of that

what is the difference is these two

classFuelStation_out_stock(models.Model):

_name="fuelstation.outstock"

_description ="OUT Fuel Record"

name = fields.Char("Customer Name")

date = fields.Datetime("Date:")

fuel_type = fields.Many2one(comodel_name ="fuelstation.fueldata",string="Fuel Type")

order_qut= fields.Float("Fuel Quantity in Ltrs")

fuel_price= fields.Float(string="Fuel Price",related='fuel_type.price')

avl_qut = fields.Float(string="Available Fuel",related='fuel_type.avl_qut')

# To Update stock

@api.depends('order_qut')

def_update_stock(self):

for rec in self:

ifrec.order_qut < rec.fuel_type.avl_qut:

rec.fuel_type.avl_qut -= rec.order_qut

else:

raiseValidationError("Fuel Out off Stock")

self.updated_stock= rec.fuel_type.avl_qut

updated_stock= fields.Float(string="Updated Stock",compute=_update_stock, store=True)

Author Best Answer

Thank you @niyas

So how can I achieve my goal

to compute the field only once on save 

And I used the same code in another module and its works below is the code of that

what is the difference is these two


classFuelStation_out_stock(models.Model):   

 _name="fuelstation.outstock"    

_description ="OUT Fuel Record"    

name = fields.Char("Customer Name")    

date = fields.Datetime("Date:")    

fuel_type = fields.Many2one(comodel_name ="fuelstation.fueldata",string="Fuel Type")    

order_qut= fields.Float("Fuel Quantity in Ltrs")    

fuel_price= fields.Float(string="Fuel Price",related='fuel_type.price')     

avl_qut = fields.Float(string="Available Fuel",related='fuel_type.avl_qut')

# To Update stock    

@api.depends('order_qut')    

def_update_stock(self):        

for rec in self:

            ifrec.order_qut < rec.fuel_type.avl_qut:

                rec.fuel_type.avl_qut -= rec.order_qut

            else:

                raiseValidationError("Fuel Out off Stock")

        self.updated_stock= rec.fuel_type.avl_qut
    

updated_stock= fields.Float(string="Updated Stock",compute=_update_stock,  store=True)


Avatar
Discard
Best Answer

Hi,

As it is a computed field, it will get triggered whenever the value of specified depends changes.

Thanks

Avatar
Discard