Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
4495 Lượt xem

I have a computed field that computes every time I refresh the page. I tried using api.depends but it computes twice, once when I fill in the dependent field and once when I save. I want to compute the once then never change it again for that record. How can I do that? I have included the code below 



    

    product_name = fields.Many2one('product.info', string='Product')
    units_sold = fields.Integer(string='Units Sold', required = True)    
    current_stock = fields.Integer("Today's Stock", compute='_compute_stock', store=True)   
    units_entered = fields.Integer('Stock Entered')     @api.depends('units_sold')   
    def _compute_stock(self):       
        for rec in self:            
            if rec.units_sold > 0 and rec.units_entered > 0:               
                rec.current_stock= rec.product_name.product_quantity - rec.units_sold + rec.units_entered           
            elif rec.units_sold > 0 and rec.units_entered == 0:               
                rec.current_stock= rec.product_name.product_quantity - rec.units_sold           
            elif rec.units_sold == 0 and rec.units_entered > 0:               
                rec.current_stock= rec.product_name.product_quantity + rec.units_entered           
            rec.product_name.product_quantity = rec.current_stock
Ảnh đại diện
Huỷ bỏ

Since you are using store=True then the computed method will be called only if one of the fields mentioned in depend api changed.

Câu trả lời hay nhất

Hello,

Try below code:

@api.depends('units_sold')
def _compute_stock(self):
for rec in self:
current_stock = 0.0
if rec.units_sold > 0 and rec.units_entered > 0:
current_stock= rec.product_name.product_quantity - rec.units_sold + rec.units_entered
elif rec.units_sold > 0 and rec.units_entered == 0:
current_stock= rec.product_name.product_quantity - rec.units_sold
elif rec.units_sold == 0 and rec.units_entered > 0:
current_stock= rec.product_name.product_quantity + rec.units_entered
rec.current_stock = current_stock
rec.product_name.product_quantity = current_stock


Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 7 22
5127
1
thg 7 21
3216
2
thg 5 24
10140
1
thg 4 21
2795
1
thg 4 24
2378