我正在研究Odoo模塊,而不是直接在模型中定義字段時,我有一個名爲schema的元組,其中包含所有字段。我在模型中定義了一個從元組中讀取每個字段的類方法,並通過內省在模型中創建它。Odoo-9:通過setattr添加字段時,「RuntimeError:最大遞歸深度超過」
@classmethod
def initialze(cls, schema):
for field in schema:
add_a_field(cls, field)
pass
pass
如可以看到的,這個方法是迭代的元組和單獨的場傳遞給另一個方法名「add_a_field(CLS,場)」。
方法'add_a_field'使用python setattr()內置方法。
def add_a_field(cls, field):
setattr(cls, field.string, field)
pass
它添加一個具有相同名稱和標籤的字段。
在我有這樣的方法我直接調用它爲下面的樣品中的類:
from openerp import fields, models
# other imports
def add_a_field(cls, field):
setattr(cls, field.string, field)
pass
schema = (
fields.Char(string='RequestID'),
fields.Float(string='Discount', default=0.00),
fields.Float(string='Subtotal', default=0.00),
)
class Batch(models.Model):
_name='batch'
@classmethod
def initialze(cls, schema):
for field in schema:
add_a_field(cls, field)
pass
pass
pass
# other model methods
Batch.initialze(schema)
在Odoo V8中工作正常,但在V9它給出了一個錯誤「」 RuntimeError:最大遞歸深度超過」
在Odoo V9 fields.py __getattr__定義如下(見https://github.com/odoo/odoo/blob/9.0/openerp/fields.py):
def __getattr__(self, name):
""" Access non-slot field attribute. """
try:
return self._attrs[name]
except KeyError:
raise AttributeError(name)
雖然__init__是如下:
def __init__(self, string=None, **kwargs):
kwargs['string'] = string
args = {key: val for key, val in kwargs.iteritems() if val is not None}
self.args = args or EMPTY_DICT
self.setup_full_done = False
在V8中fields.py __init__定義如下:
def __init__(self, string=None, **kwargs):
kwargs['string'] = string
attrs = {key: val for key, val in kwargs.iteritems() if val is not None}
self._attrs = attrs or EMPTY_DICT
錯誤是:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in initialze
File "<stdin>", line 2, in add_a_field
File "openerp/fields.py", line 343, in __getattr__
return self._attrs[name]
File "openerp/fields.py", line 343, in __getattr__
return self._attrs[name]
File "openerp/fields.py", line 343, in __getattr__
return self._attrs[name]
File "openerp/fields.py", line 343, in __getattr__
return self._attrs[name]
File "openerp/fields.py", line 343, in __getattr__
return self._attrs[name]
:
:
:
:
RuntimeError: maximum recursion depth exceeded while calling a Python object
任何想法,這個應該怎麼解決?