- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在开发一个 Odoo 模块,其中不是直接在模型中定义字段,而是有一个名为 schema 的元组,其中包含所有字段。我在模型中定义了一个类方法,该方法从元组中读取每个字段并通过内省(introspection)在模型上创建它。
@classmethod
def initialze(cls, schema):
for field in schema:
add_a_field(cls, field)
pass
pass
可以看出,该方法正在元组上进行迭代,并将单个字段传递给另一个方法名称“add_a_field(cls, field)”。
“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
知道如何解决这个问题吗?
最佳答案
经过一些调试和查看 fields.py ,很明显,Odoo 不再希望模块/应用程序代码使用点表示法访问初始化参数(又名初始化参数),因此使用 field.string 访问字段或 field.required 的名称来查明该字段是否是必需的,而不是不再是一段有效的代码。相反,现在应该从名为 args 的字典类型字段实例变量访问所有初始化参数。
当我在以下代码中访问 field.string 时,出现了运行时错误:
def add_a_field(cls, field):
setattr(cls, field.string, field)
pass
我现在将代码修改如下:
def add_a_field(cls, field):
setattr(cls, field.args['string'], field)
pass
args 和 _attrs 在 fields.py 中定义如下:
_slots = {
'args': EMPTY_DICT, # the parameters given to __init__()
'_attrs': EMPTY_DICT, # the field's non-slot attributes
早期的 Odoo v8 中没有参数,而 _attrs 定义如下:
_slots = {
'_attrs': EMPTY_DICT, # dictionary of field attributes; it contains:
# - all attributes after __init__()
# - free attributes only after set_class_name()
因此,结论是模块/应用程序代码现在应该使用 field.args['string'] 或 field.args['required'] 而不是直接使用点表示法,即 field.string 或 field.required
关于python - Odoo-9:通过 setattr 添加字段时出现 "RuntimeError: maximum recursion depth exceeded",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33578426/
运行 server.py 文件时出现错误 File "C:\Users\nawin\AppData\Local\Programs\Python\Python38\lib\site-packages\s
我在我的 PC 上运行 cifar10 网络,在完成训练和运行评估脚本后出现以下错误: 2016-06-01 14:37:14.238317: precision @ 1 = 0.000 Traceb
我已经编写了一段代码来训练Guassian过程回归模型来预测年龄。我已经编写了以下代码,并且运行良好:。但我注意到,每个纪元都输入了相同的数据,我认为这可能会导致过度拟合,所以我想使用Mini Bat
我有一个数据集,其中类值从 -2 到 2 步 (i.e., -2,-1,0,1,2)其中 9 标识未标记的数据。 使用一种热编码 self._one_hot_encode(labels) 我收到以下错
我是 Phoenix Framework 的新用户,我正在尝试设置一个简单的 HTTP POST 服务,该服务对传入数据执行计算并返回结果,但出现以下错误: ** (RuntimeError) exp
为什么这段代码运行良好并且不抛出异常? def myzip(*args): iters = [iter(arg) for arg in args] try: while
今天,当我开始编写我的网页时,它工作正常,但突然我的 css 文件无法工作。我的更改没有更新。读了一点之后,我读到我应该清理我的缓存。我这样做了,之后当我运行我的网页并单击任何按钮时,我会在我的控制台
我使用描述符编写了一个 Circle 类,允许用户设置圆的 x、y 和 r 的值,并检查 x 和 y 的值是否为整数。如果用户输入非整数,则会引发 TypeError,现在我想制作另一个描述符类,允许
代码: # callee.py import signal import sys import time def int_handler(*args): for i in range(10):
将 RuntimeError 异常用于一般应用程序是否可以接受? raise RuntimeError('config file is missing host address') 我有一些代码会遇到
Pytorch测试神经网络时出现“RuntimeError: Error(s) in loading state_dict for Net” 解决方法: ?
得到标题中提到的错误。下面提到的函数由通过POST api调用的另一个函数调用。 打印语句下方的行上有错误。不知道该错误意味着什么,为什么会出现。一周前使用的相同代码。 def remove_indi
我正在尝试计算 PyTorch 中变量的梯度。然而,有一个运行时错误告诉我输出和梯度的形状必须相同。然而,就我而言,输出和梯度的形状不能相同。这是我要重现的代码: import numpy as np
我正在尝试在 ExpandableListView 中查看数据库中的数据(我首先尝试让它使用硬编码字符串)。 我使用了以下示例:CodeWiki ExpandableListView 但是当我点击一个
import asyncio import json import websockets from mongodb import * class WebSocketRe
我正在尝试同时使用多处理和 matplotlib。 我正在创建一个标准的 Pool,添加与 apply_async 的工作,并使用 apply_async 的回调函数更新 GUI,它运行于Pool 的
我正在阅读 Python 3 的文档 here : If a generator code directly or indirectly raises StopIteration, it is con
我无法使用 BeautifulSoup 避免最大递归深度 Python RuntimeError。 我正在尝试递归嵌套的代码部分并提取内容。美化后的 HTML 看起来像这样(不要问为什么它看起来像这样
我知道已经有人问过这个问题,但我仍然无法找到解决方案。 我想在自定义数据集上使用 gensim 的 word2vec,但现在我仍在弄清楚数据集必须采用什么格式。我看了this post其中输入基本上是
我在 Colab Pro+(使用高 RAM 选项)上运行神经网络时发现了这个问题。 运行时错误:CUDA 内存不足。尝试分配 8.00 GiB(GPU 0;15.90 GiB 总容量;12.04 Gi
我是一名优秀的程序员,十分优秀!