Skip to Content
Menu
This question has been flagged
1 Reply
2403 Views

Hello there,

I need to override the write function and from inside it, to write in an other record (after the current record has been saved). This code creates a "maximal recursion depth exceeded" because the write function is calling itself from the write function. I read on stark overflow that you can get around this by doing something like this: 

@api.multi
def write(self, vals):
     if 'custom_field' not in vals:
         vals['custom_field'] = 37
     return super(self).write(vals)

but this only writes in the current record. I need the values of some other records (of the same model -hr.expense-) to be updated as well. If using "update" instead of "write, it doesn't update the field total amount. Here is my code.

How can i fix this? 


[infos: v9 community, module concerned: hr. expense]

@api.multi    
def write(self, vals):
     self.update_other_dest_ids()
     value = super(Expenses, self).write(vals)
     self.search_and_modify_connected_travel()
     for tagx in self.expenses_summary_ids
         if tagx.time is not '24:00':
             exps = self.find_all_expenses_on_same_day(tagx.day)
             for each in exps:
                each._update_unit_price()
     return value


@api.one    
@api.depends('expenses_summary_ids.amount_due')
def _update_unit_price(self):
    if self.expenses_calculator:
         table_sum = 0.0
         for expense in self:
             for line in expense.expenses_summary_ids:
                 table_sum += line.amount_due
                 if self.calculated_unit_amount != table_sum:
                     self.write({'calculated_unit_amount' : table_sum})
                 if self.unit_amount != table_sum:
                     self.write({'unit_amount' : self.calculated_unit_amount})
                    
Avatar
Discard
Best Answer

if you are using api.one you dont need the "for expense in self:"

@api.one    
@api.depends('expense_summary_ids','expenses_summary_ids.amount_due')
def _update_unit_price(self):
    if self.expenses_calculator:
         table_sum = 0.0
         for line in self.expenses_summary_ids:
             table_sum += line.amount_due
             if self.calculated_unit_amount != table_sum:
                 self.calculated_unit_amount = table_sum
             if self.unit_amount != table_sum:
                 self.unit_amount = self.calculated_unit_amount
maybe calculated_unit_amount should be on the depends too.

Avatar
Discard
Related Posts Replies Views Activity
1
Sep 18
2068
1
Mar 15
4271
5
Nov 22
4769
1
Mar 16
2801
0
Jan 16
2422