python - Odoo 13 one2many 字段上的错误计算函数
问题描述
我想将另一个模型中的 One2Many 字段的一部分与另一个模型中的正确匹配同步。我在 project.project 中有一个 one2many 字段,其中包括字段 emp_id(hr.employee) 和 role_id(project.roles)。现在在模型 account.analytic.line(第二张图片)中已经有合适的员工,所以我只想计算正确的角色并将其显示在正确的字段中。但就我现在而言,我得到了单例错误,我知道它导致许多对象的原因并且它只需要一个对象。但老实说,我不知道如何计算正确项目的正确角色。
这是我的 One2Many 字段 (project.project) 有很多项目,我需要使用完全正确的时间表获得正确的项目。 角色字段(最后一行) account.analytic.line
我的计算功能:
class RoleSync(models.Model):
_inherit = 'account.analytic.line'
#_inherits = {'project.project': 'list_id'}
role_field_id = fields.Many2one(string='Role', compute='_compute_role')
def _compute_role(self):
emp = []
# to get the one2many field with the correct project
list = self.env['project.project'].search(
[('name', '=', self.project_id.name)]).list_id
# to get the right timesheet for the correct project
timesheet = self.env['account.analytic.line'].search(
[('project_id.name', '=', self.name)])
# iterate through timesheet and get name of employee
for val in timesheet:
emp.append(val.name)
# nested loop for list with employee names and the one2many field
for a in emp:
for b in list:
# if the names are the same
if a.emp == b.emp_id.name:
# take the role and display it in the field
self.role_field_id = b.emp_id.role_id
错误:
Error:
Odoo Server Error
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/api.py", line 745, in get
value = self._data[field][record._ids[0]]
KeyError: 18
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1004, in __get__
value = env.cache.get(record, self)
File "/usr/lib/python3/dist-packages/odoo/api.py", line 751, in get
raise CacheMiss(record, field)
odoo.exceptions.CacheMiss: ('account.analytic.line(18,).role_field_id', None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/models.py", line 5101, in ensure_one
_id, = self._ids
ValueError: too many values to unpack (expected 1)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/odoo/http.py", line 624, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 310, in _handle_exception
raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
File "/usr/lib/python3/dist-packages/odoo/tools/pycompat.py", line 14, in reraise
raise value
File "/usr/lib/python3/dist-packages/odoo/http.py", line 669, in dispatch
result = self._call_function(**self.params)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 350, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/service/model.py", line 94, in wrapper
return f(dbname, *args, **kwargs)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 339, in checked_call
result = self.endpoint(*a, **kw)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 915, in __call__
return self.method(*args, **kw)
File "/usr/lib/python3/dist-packages/odoo/http.py", line 515, in response_wrap
response = f(*args, **kw)
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 1285, in search_read
return self.do_search_read(model, fields, offset, limit, domain, sort)
File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 1304, in do_search_read
return Model.web_search_read(domain, fields, offset=offset, limit=limit, order=sort)
File "/usr/lib/python3/dist-packages/odoo/addons/web/models/models.py", line 39, in web_search_read
records = self.search_read(domain, fields, offset=offset, limit=limit, order=order)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 4951, in search_read
result = records.read(fields)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 2965, in read
vals[name] = convert(record[name], record, use_name_get)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 5731, in __getitem__
return self._fields[key].__get__(self, type(self))
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 2333, in __get__
return super().__get__(records, owner)
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1028, in __get__
self.compute_value(recs)
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1113, in compute_value
records._compute_field_value(self)
File "/usr/lib/python3/dist-packages/odoo/models.py", line 4003, in _compute_field_value
getattr(self, field.compute)()
File "/opt/Odoo/Custom_Addon/project_addon/models/analytic_class.py", line 18, in _compute_role
[('project_id.name', '=', self.name)])
File "/usr/lib/python3/dist-packages/odoo/fields.py", line 988, in __get__
record.ensure_one()
File "/usr/lib/python3/dist-packages/odoo/models.py", line 5104, in ensure_one
raise ValueError("Expected singleton: %s" % self)
ValueError: Expected singleton: account.analytic.line(18, 12, 9, 6)
解决方案
推荐阅读
- android - Android:以编程方式更新的联系人未与其他应用同步
- google-sheets - 谷歌表格根据单元格中的文本更改背景
- python - 无法将值转换为轴单位' - 在为 df 创建绘图时
- angular - Angular 无法在表格上以 JSON 格式显示数据
- javascript - 无法使用 Jquery 3.4.1、c# mvc 设置下拉列表的选定属性
- json - 如果我使用 JSON Schema 的 if-else 条件,有没有办法引发错误,其中提到 JSON 中存在的附加键的名称?
- python - Windows 上的 Python 打开新窗口
- postgresql - 使用非分区表中的数据回填分区表的最佳方法是什么?(postgres 12)
- javascript - 使用选择器正确查找文章的问题
- ios - 无需打开 Gallery Swift 即可获取视频或视频信息