ValueError预期的单例,Odoo8 [英] ValueError Expected singleton , Odoo8

查看:67
本文介绍了ValueError预期的单例,Odoo8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在Ubuntu 14.04上的Odoo8中开发一个模块.在保存基于One2many字段的表单记录时,我遇到一个奇怪的问题.错误显示

I have been working on a module in Odoo8 on Ubuntu 14.04. I have come up to a strange issue when saving a form record based on some One2many fields. The error says

ValueError

Expected singleton: hr.employee.pay.change(84, 85) 

我的Python代码如下

My Python code is as below

class hr_employee_pay_change(models.Model):

    _name='hr.employee.pay.change'
    hr_payroll_change_ids = fields.Many2one("employee.salary.change", "Employee", ondelete="cascade")

    @api.onchange('emp_basic', 'emp_allowance')
    @api.depends('emp_basic', 'emp_allowance')
    def _current_total(self):
        self.emp_current_total = self.emp_basic + self.emp_allowance



    @api.onchange('emp_propose_allowance', 'emp_propose_basic')
    @api.depends('emp_propose_allowance', 'emp_propose_basic')
    def _proposed_total(self):
        data_val={}
        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_val in res:
               max_sal=data_val[0]
               min_sal=data_val[1]
           if not min_sal < self.emp_propose_total < max_sal: 
              self.emp_propose_basic = 0.0
              self.emp_propose_allowance = 0.0
              return {'warning':{'title':'warning','message':'Out of Range, Proposed Total must be in between "'+str(max_sal)+'"to"'+str(min_sal)+'"'}}
        else:
           cr.execute("select wage from hr_contract where employee_id=0")    




    @api.onchange('employee_name')
    @api.depends('employee_name')
    def get_data(self):
        data={}
        cr=self._cr
        uid=self._uid
        ids=self._ids     
        value=int(self.employee_name)        
        if(self.employee_name):  
           cr.execute("select wage,allowance from hr_contract where employee_id ='"+str(value)+"'")
           res=cr.fetchall()
           for data in res:
               self.emp_basic=data[0]
               self.emp_allowance = data[1]

        else:     
           cr.execute("select wage,allowance from hr_contract where employee_id=0")   



    employee_name = fields.Many2one('hr.employee', 'Employee Name', required=True )
    zeo_number = fields.Char(related='employee_name.zeo_number', string='ZEO Number', readonly=True )
    emp_basic = fields.Float('Basic Salary',  compute='get_data',readonly=True, store=True )
    emp_allowance = fields.Float('Allowance', compute='get_data',readonly=True, store=True )
    emp_current_total = fields.Float('Totals', compute='_current_total', store=True, track_visibility='always')
    emp_propose_basic = fields.Float('Proposed Basic')
    emp_propose_allowance = fields.Float('Proposed Allowance')
    emp_propose_total = fields.Float('Proposed Totals', compute='_proposed_total', store=True, track_visibility='always')

我无法解决此问题.我试图删除字段的'readonly = True'属性,问题已解决,但我需要将它们设置为readonly.希望提出建议

I am unable to get this issue . I tried to remove the 'readonly=True' property of the field and the issue gets fixed but I need to have them as readonly . Hopes for suggestion

推荐答案

预期的单例:

类方法需要单个调用对象(单个可浏览记录)来调用该方法,并假设它将通过多个调用对象(可浏览记录集)进行调用,则该方法将无法执行来确定应针对哪个对象进行处理,因此会产生错误预期的单例.

Class methods required single invoking object (Single Browsable Record) to invoke the method and suppose it will call by multiple invoking objects (Browsable Recordsets) then method is not able to identify for which object it should process, therefore it will raise an error Expected Singleton.

新的API装饰器用于定义方法调用模式,无论方法仅允许单个对象还是多个对象来调用此方法.

New API decorator is used to define method calling pattern whether methods allows only single object or multiple objects to invoke this method.

@ api.one

此装饰器为您自动在RecordSet的Records上循环.将自己重新定义为当前记录

This decorator loops automatically on Records of RecordSet for you. Self is redefined as current record

注意: 注意:返回值放在列表中.网络客户端并不总是支持此功能,例如按键动作 方法.在这种情况下,您应该使用 @ api.multi 装饰您的方法,并可能在其中调用 self.ensure_one() 方法定义.

Note: Caution: the returned value is put in a list. This is not always supported by the web client, e.g. on button action methods. In that case, you should use @api.multi to decorate your method, and probably call self.ensure_one() in the method definition.

@ api.multi

Self将是当前RecordSet,无需迭代.这是默认行为(多个可浏览对象).返回非专有类型数据(列表,字典,函数)的方法必须使用 @ api.multi

Self will be the current RecordSet without iteration. It is the default behavior (multiple browsable objects). Methods which returns non premitive type data(list, dictionary, function) must be decorated with @api.multi

@ api.model

此装饰器会将旧的API调用转换为装饰后的函数,并转换为新的API签名.允许礼貌 迁移代码.在此装饰器装饰的方法中,Self不包含任何记录/记录集.

This decorator will convert old API calls to decorated function to new API signature. It allows to be polite when migrating code. Self does not contain any record/recordset in methods which are decorated by this decorator.

所以只需这样打电话

self.env ['model_name'].method_name(参数)

您应该尝试关注

class hr_employee_pay_change(models.Model):
    _name='hr.employee.pay.change'
    hr_payroll_change_ids = fields.Many2one("employee.salary.change", "Employee", ondelete="cascade")

    @api.onchange('emp_basic', 'emp_allowance')
    @api.depends('emp_basic', 'emp_allowance')
    def _current_total(self):
        for rec in self:
            rec.emp_current_total = rec.emp_basic + rec.emp_allowance

    @api.onchange('emp_propose_allowance', 'emp_propose_basic')
    @api.depends('emp_propose_allowance', 'emp_propose_basic')
    def _proposed_total(self):
        for rec in self:
            data_val={}
            rec.emp_propose_total = rec.emp_propose_basic + rec.emp_propose_allowance
            cr=self._cr
            uid=self._uid
            ids=self._ids  
            val=int(rec.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_val in res:
                   max_sal=data_val[0]
                   min_sal=data_val[1]
               if not min_sal < self.emp_propose_total < max_sal: 
                  self.emp_propose_basic = 0.0
                  self.emp_propose_allowance = 0.0
                  return {'warning':{'title':'warning','message':'Out of Range, Proposed Total must be in between "'+str(max_sal)+'"to"'+str(min_sal)+'"'}}
            else:
               cr.execute("select wage from hr_contract where employee_id=0")    

    @api.onchange('employee_name')
    @api.depends('employee_name')
    def get_data(self):
        for rec in self:
            data={}
            cr=self._cr
            uid=rec._uid
            ids=rec._ids     
            value=int(rec.employee_name)        
            if(rec.employee_name):  
               cr.execute("select wage,allowance from hr_contract where employee_id ='"+str(value)+"'")
               res=cr.fetchall()
               for data in res:
                   rec.emp_basic=data[0]
                   rec.emp_allowance = data[1]

            else:     
               cr.execute("select wage,allowance from hr_contract where employee_id=0")   

这篇关于ValueError预期的单例,Odoo8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆