python – 重写odoo 8中的write()方法导致RuntimeError:超出最大递归深度

前端之家收集整理的这篇文章主要介绍了python – 重写odoo 8中的write()方法导致RuntimeError:超出最大递归深度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这似乎是一个重要的简单问题,但不知怎的,我无法找到解决方案.

当我按下保存按钮时,将执行write方法.我想在每次调用write方法时更改当前模型的值(或调用函数),所以我重写了我的模型的write()方法

@api.multi
def write(self,vals):
    self.flaeche = 37
    return super(lager,self).write(vals)

(模型是lager.py,字段是flaeche = fields.Float(string = u“Fläche(m²)”))

错误:RuntimeError:超出最大递归深度

Traceback (most recent call last):
  File "/home/tertia/workspace/odoo8/openerp/http.py",line 530,in _handle_exception
    return super(JsonRequest,self)._handle_exception(exception)
  File "/home/tertia/workspace/odoo8/openerp/http.py",line 567,in dispatch
    result = self._call_function(**self.params)
  File "/home/tertia/workspace/odoo8/openerp/http.py",line 303,in _call_function
    return checked_call(self.db,*args,**kwargs)
  File "/home/tertia/workspace/odoo8/openerp/service/model.py",line 113,in wrapper
    return f(dbname,**kwargs)
  File "/home/tertia/workspace/odoo8/openerp/http.py",line 300,in checked_call
    return self.endpoint(*a,**kw)
  File "/home/tertia/workspace/odoo8/openerp/http.py",line 796,in __call__
    return self.method(*args,line 396,in response_wrap
    response = f(*args,**kw)
  File "/home/tertia/workspace/odoo8/openerp/addons/web/controllers/main.py",line 936,in call_kw
    return self._call_kw(model,method,args,kwargs)
  File "/home/tertia/workspace/odoo8/openerp/addons/web/controllers/main.py",line 928,in _call_kw
    return getattr(request.registry.get(model),method)(request.cr,request.uid,**kwargs)
  File "/home/tertia/workspace/odoo8/openerp/api.py",line 241,in wrapper
    return old_api(self,line 363,in old_api
    result = method(recs,**kwargs)
  File "/home/tertia/workspace/odoo8/custom_modules/g4e_config/lager.py",line 225,in write
    self.flaeche = 37
  File "/home/tertia/workspace/odoo8/openerp/fields.py",line 853,in __set__
    record.write({self.name: self.convert_to_write(value)})
  File "/home/tertia/workspace/odoo8/openerp/api.py",line 239,in wrapper
    return new_api(self,**kwargs)

.....

  File "/home/tertia/workspace/odoo8/custom_modules/g4e_config/lager.py",line 224,in write
    super(lager,self).write(vals)
  File "/home/tertia/workspace/odoo8/openerp/api.py",line 546,in new_api
    result = method(self._model,cr,uid,self.ids,**kwargs)
  File "/home/tertia/workspace/odoo8/openerp/addons/mail/mail_thread.py",line 435,in write
    self.message_auto_subscribe(cr,ids,values.keys(),context=context,values=values)
  File "/home/tertia/workspace/odoo8/openerp/api.py",line 1883,in message_auto_subscribe
    header_subtype_ids = subtype_obj.search(cr,['|',('res_model','=',False),('parent_id.res_model',self._name)],context=context)
  File "/home/tertia/workspace/odoo8/openerp/api.py",**kwargs)
  File "/home/tertia/workspace/odoo8/openerp/models.py",line 1639,in search
    return self._search(cr,user,offset=offset,limit=limit,order=order,count=count)
  File "/home/tertia/workspace/odoo8/openerp/api.py",line 4621,in _search
    query = self._where_calc(cr,line 4444,in _where_calc
    e = expression.expression(cr,domain,self,context)
  File "/home/tertia/workspace/odoo8/openerp/osv/expression.py",line 646,in __init__
    self.parse(cr,context=context)
  File "/home/tertia/workspace/odoo8/openerp/osv/expression.py",line 847,in parse
    right_ids = comodel.search(cr,[(path[1],operator,right)],line 4615,in _search
    self.check_access_rights(cr,access_rights_uid or user,'read')
  File "/home/tertia/workspace/odoo8/openerp/api.py",line 3477,in check_access_rights
    return self.pool.get('ir.model.access').check(cr,self._name,operation,raise_exception)
  File "/home/tertia/workspace/odoo8/openerp/api.py",**kwargs)
  File "<string>",line 2,in check
  File "/home/tertia/workspace/odoo8/openerp/tools/cache.py",line 117,in lookup
    r = d[key]
  File "/home/tertia/workspace/odoo8/openerp/tools/func.py",line 66,in wrapper
    return func(self,**kwargs)
  File "/home/tertia/workspace/odoo8/openerp/tools/lru.py",line 38,in __getitem__
    self[a[0]] = a[1]
  File "/home/tertia/workspace/odoo8/openerp/tools/func.py",line 44,in __setitem__
    del self[obj]

当我想调用较大模型的函数时,也会发生同样的情况.
我究竟做错了什么?

解决方法

问题是通过编写self.flaeche = 37,您正在更改记录,这意味着在模型上隐式调用write()方法.当你从write()调用write()时,你最终会得到递归.

你可以做类似的事情:

@api.multi
def write(self,vals):
    vals['flaeche'] = 37
    return super(lager,self).write(vals)

这种方式没有额外的写入 – 您只需更改即将发生的写入的值.

如果您想允许人们显式覆盖37的值,您可以这样做:

@api.multi
def write(self,vals):
    if 'flaeche' not in vals:
        vals['flaeche'] = 37
    return super(lager,self).write(vals)
原文链接:https://www.f2er.com/python/241900.html

猜你在找的Python相关文章