Change a field value within an If statement in Odoo 8

微笑、不失礼 提交于 2019-12-25 01:33:18

问题


I have been using Odoo 8 with Ubuntu 14.04. I have an onchange function and under that an If statement in which I am trying to change a field value but it does not get change . All I need is to assign the null value or 0.0 to the field but failed. My Python code is below:

def _proposed_total(self):
    print self.emp_propose_allowance
    self.emp_propose_total = self.emp_propose_basic + self.emp_propose_allowance
    cr=self._cr
    uid=self._uid
    ids=self._ids 
    val=int(self.employee_name)
    if(val):
       cr.execute("select max_salary,min_salary from hr_job where id in (select job_id from hr_employee where id='"+str(val)+"')")
       res=cr.fetchall()
       for data in res:
               max_sal=data[0]
               min_sal=data[1]
           if(self.emp_propose_total>max_sal): 
              raise osv.except_osv('out of range','Proposed Total must be in between "'+str(max_sal)+'" to "'+str(min_sal)+'"')            
              self.emp_propose_basic=0.0
              self.emp_propose_allowance=0.0

           elif(self.emp_propose_total<min_sal):
              raise osv.except_osv('out of range','Proposed Total must be in between "'+str(max_sal)+'" to "'+str(min_sal)+'"')
              self.emp_propose_basic=0
              self.emp_propose_allowance=0  
        else:
           cr.execute("select wage from hr_contract where employee_id=0") 

I want to change the self.emp_propose_basic and self.emp_propose_allowance . I am not sure what I am doing wrong here. Plz guide me or point me my mistake to achieve this


回答1:


If you raise any exception then your all previous statements which are uncommit will be rolled back. So value will not be set to the fields if you raise from method.

Choose either raise or set default value there, you should return warning if you want, it will not break your execution.

For returning warning check the onchange method of price list in sale order.




回答2:


You have to raise the exception at the end! Raising an exception aborts the current execution of your code, this is why your values don't get changed. These statements aren't reached.

You could rewrite your ifs in a more Pythonic style such as:

if not (min_sal < self.emp_propose_total < max_sal):
    self.emp_propose_basic = 0.0
    self.emp_propose_allowance = 0.0
    raise osv.except_osv('out of range','Proposed Total must be in between %s to %s' % (max_sal, min_sal))


来源:https://stackoverflow.com/questions/31048895/change-a-field-value-within-an-if-statement-in-odoo-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!