这是我的代码。我想在定义其模型类之前获取当前记录的 id_employee:
def _get_state(self, cr, uid, context=None):
idemployee = ""
for adv in self.browse(cr, uid, ids, context):
id_employee = adv.id_employee
if id_employee is None:
idemployee = _default_employee(self, cr, uid, context=None)
else:
idemployee = id_employee
sql = " SELECT C.id AS id, C.sequence, C.name \
FROM wf_group_member A \
LEFT JOIN wf_group B ON B.id = A.group_id \
LEFT JOIN wf_process BB ON BB.id = B.process_id \
LEFT JOIN wf_state C ON C.group_id = B.id \
LEFT JOIN hr_employee D ON D.id = A.member_id \
WHERE LOWER(code) = 'ca' AND member_id = %s ORDER BY sequence "
res = []
cr.execute(sql, [(idemployee)])
ardata = cr.fetchall()
for data in ardata:
res.append((data[1], data[2]))
return res
这是我放在 _get_state 函数之后的模型类
class cashadvance(osv.osv):
_name = 'ga.cashadvance'
_columns = {
'id_user' : fields.many2one('res.users', string='User', required=True, readonly=True),
'state' : fields.selection(_get_state, 'Status', readonly=True),
'id_employee' : fields.many2one('hr.employee', string='Employee', required=True, readonly=True),
}
当我调用 _get_state 函数时,它引发了错误:
Error details:
global name 'ids' is not defined
None" while parsing /opt/custom-addons/comben/views/cashadvance_view.xml:4, near
<record id="cashadvance_list" model="ir.ui.view">
<field name="name">cashadvance_list</field>
<field name="model">ga.cashadvance</field>
<field name="arch" type="xml">
<tree string="Cashadvance List">
<field name="id_employee"/>
<field name="category_id"/>
<field name="est_date_from" string="Est Date From"/>
<field name="est_date_to" string="Est Date To"/>
<field name="description"/>
<field name="state"/>
</tree>
</field>
</record>
谁能帮帮我,谢谢
请您参考如下方法:
_get_state 必须是类的一部分,您才能在类外使用它,您传入的 self 不会被视为 python self即当前实例,它被视为函数的正常参数。
Python 不会为您传递 self ,让我稍微偏离一下并重现您遇到的错误。看看这段代码
def func(self,cr, uid, ids, context=None):
pass
class func_test:
def __init__(self):
func(1, 1, 1, context=1)
a = func_test()
这是引发的错误(确保使用 python2 运行它)
Traceback (most recent call last):
File "script.py", line 8, in <module>
a = func_test()
File "script.py", line 6, in __init__
func(1, 1, 1, context=1)
TypeError: func() takes at least 4 arguments (4 given)
python 实际上是正确的,尽管它具有误导性,该函数需要至少四个参数,因为上下文是一个关键字参数,并且在函数定义时已被填充,但它仍然缺少一个参数,因为该函数在外部一个类,因此通常称为 self 的第一个参数被视为普通参数(因为它是一个函数)而不是方法(类中的函数)。所以从左到右 self, cr, uid 会填成 1 1 1 离开 ids 并且此时 python 会大声求助,因为如果我们将该函数移到类中并调用它,则因为找不到该参数的值使用 self.func,当前实例会自动为我们传递。
class func_test:
def func(self,cr, uid, ids, context=None):
pass
def __init__(self):
self.func(1, 1, 1, context=1)
a = func_test()
当然你仍然可以使用 func(self, 1, 1, 1, context=1) 但这会违背方法的目的
但是请注意,python3 比 python2 更聪明,并且可以更好地处理这种情况,这是 python3 的回溯
Traceback (most recent call last):
File "script.py", line 8, in <module>
a = func_test()
File "script.py", line 6, in __init__
func(1, 1, 1, context=1)
TypeError: func() missing 1 required positional argument: 'ids'
它清楚地告诉我们在函数调用中没有为 ids 提供值
所以回到 Odoo 你的代码应该是这样的
from openerp.osv import osv, fields
class cashadvance(osv.osv):
_name='comben.cashadvance'
def _get_state(self, cr, uid, context=None):
idemployee = ""
ids = self.search(cr, uid, [], context=context) # you probably want to search for all the records
for adv in self.browse(cr, uid, ids, context=context):
id_employee = adv.id_employee
if id_employee is None:
idemployee = self._default_employee(cr, uid, ids, context=None) # might raise singleton error if more than one record is returned
else:
idemployee = id_employee.id
sql = " SELECT C.id AS id, C.sequence, C.name \
FROM wf_group_member A \
LEFT JOIN wf_group B ON B.id = A.group_id \
LEFT JOIN wf_process BB ON BB.id = B.process_id \
LEFT JOIN wf_state C ON C.group_id = B.id \
LEFT JOIN hr_employee D ON D.id = A.member_id \
WHERE LOWER(code) = 'ca' AND member_id = %s ORDER BY sequence "
res = []
cr.execute(sql, [(idemployee)])
ardata = cr.fetchall()
for data in ardata:
res.append((data[1], data[2]))
return res
_columns = {
'id_user': fields.many2one('res.users', string='User', required=True, readonly=True),
'state': fields.selection(_get_state, string='Status', readonly=True),
'id_employee': fields.many2one('hr.employee', string='Employee', required=True, readonly=True),
}
同时将 _default_employee 移动到类体中,这样你就可以这样调用它
self._default_employee(cr, uid, context=None) # python implicitly passes self for you
xml View
<record id="cashadvance_list" model="ir.ui.view">
<field name="name">cashadvance list</field>
<field name="model">comben.cashadvance</field>
<field name="arch" type="xml">
<tree string="Cashadvance List">
<field name="id_employee"/>
<field name="id_user"/>
<field name="state"/>
</tree>
</field>
</record>
