콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다



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. 



아바타
취소
작성자

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)

작성자 베스트 답변

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)


아바타
취소
베스트 답변

Hi,

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

Thanks

아바타
취소
관련 게시물 답글 화면 활동
1
1월 22
2732
0
4월 22
2785
1
1월 22
4562
1
12월 21
5132
1
8월 24
2786