Odoo:_get_state()在xml视图中至少接受4个参数(给定4个) [英] Odoo: _get_state() takes at least 4 arguments (4 given) in xml view

查看:87
本文介绍了Odoo:_get_state()在xml视图中至少接受4个参数(给定4个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是mycode.我想在定义其模型类之前获取当前记录的id_employee:

Here is mycode. I want to get id_employee of current record before define its model class:

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函数之后的模型类

and this is my model class that I put it after _get_state function

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函数时,它引发了错误:

When I call _get_state function, it raised error :

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必须是类的一部分,您才能在类外使用它,而您要传递给它的自我不会被视为python self,即当前实例,它被视为函数的常规参数.

_get_state has to be part of the class for you to use it not outside the class, the self you're passing in there is not seen as the python self i.e the current instance, it's seen as a normal argument of the function.

Python不会为您传递自我,让我稍微偏离一下并重现您得到的错误.看这段代码

Python isn't passing in self for you, let me deviate a little and reproduce the error you're getting. look at this code

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运行此错误)

This is the Error raised (make sure you run this with 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实际上是正确的,即使它具有误导性,该函数至少需要四个参数,因为context是一个关键字参数,并且在函数定义时已被填充,但是它仍然缺少一个参数,因为该函数位于外部一个类,因此通常称为self的第一个参数被视为普通参数(由于它是一个函数)而不是方法(类中的函数).所以从左到右selfcruid将被填充为1 1 1并离开ids,此时python将大声呼救,因为未找到任何值对于该参数,如果将函数移到类中并用self.func调用,则当前实例将自动为我们传递.

python is actually correct even though it's misleading, the function expects at least four arguments because context is a keyword argument and has been filled in at the time of the function definition, but it's still missing one parameter, because the function is outside a class so the first argument which is usually called self is taken as a normal parameter (since it's a function) not a method (function in a class). so starting from left to right self, cr, uid would be filled in as 1 1 1 leaving ids and at this point python would cry out for help because no value was found for that argument, if we move that function into the class and call it with self.func, the current instance automatically gets passed for us.

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),但这将破坏方法的目的

Of course you could still have func(self, 1, 1, 1, context=1) but that would be defeating the purpose of methods

但是请注意,python3比python2更智能,并且在这种情况下的处理效果更好,这是python3的回溯

But note that python3 is smarter and handles this scenario better than python2, this is a python3 traceback

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提供任何值

It plainly tells us that no value was provided for ids in the function call

所以回到Odoo,您的代码应该像这样

so going back to Odoo your code should look like this

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移到类主体中,因此您可以这样称呼它

and also move _default_employee into the class body, so you can call it like this

self._default_employee(cr, uid, context=None) # python implicitly passes self for you

xml视图

<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>

这篇关于Odoo:_get_state()在xml视图中至少接受4个参数(给定4个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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