Odoo 13 错误:交换到树视图时需要单例

Posted

技术标签:

【中文标题】Odoo 13 错误:交换到树视图时需要单例【英文标题】:Odoo 13 Error: Expected singleton when swapping to tree view 【发布时间】:2021-11-15 18:12:56 【问题描述】:

此错误返回 2 条记录的字段只能容纳 1 条。当我想从日历视图切换到树视图时会发生这种情况。我认为这是因为 compute 函数,但我不明白我的代码中的错误在哪里。我为 2 个不同的模型计算字段:

类休假:

from odoo import api, fields, models, tools
from odoo.exceptions import UserError, AccessError, ValidationError
from odoo.tools import date_utils


class LeaveOff(models.Model):
    _name = 'project.leaveoff'

    employ_id = fields.Many2one('hr.employee', required='1')

    begin_date = fields.Date('from', required='1')

    # only_oneday = fields.Selection(
    # [('Typ1', 'More than one day'), ('Typ2', 'One day')], default='Typ1')

    end_date = fields.Date('to')

    day_hours = fields.Float('Hours per day', default='8.00', required='1')

    requested_hours = fields.Float(
        'LeaveOff Hours', compute='_compute_requested_hours')

    def _compute_requested_hours(self):

        for record in self:
            if record.employ_id.id:
                start = self.begin_date
                end = self.end_date
                delta = end - start
                self.requested_hours = self.day_hours * (delta.days + 1)

我的 XML:


<?xml version="1.0" encoding="utf-8"?>

<!-- Action for leaveoffs -->
<odoo>
  <record id="action_for_leaveoff" model="ir.actions.act_window">
          <field name="name">LeaveOff</field>
          <field name="res_model">project.leaveoff</field>
          <field name="view_mode">calendar,tree,form</field>
          <field name="help" type="html">
            <p class="o_view_nocontent_smiling_face">
              Here, you can enter LeaveOff Times from your employees!
            </p>
          </field>
  </record>
  <!-- form view -->
  <record id="project_leaveoff_form_view" model="ir.ui.view">
          <field name="name">view.project.leaveoff.form</field>
          <field name="model">project.leaveoff</field>
          <field name="arch" type="xml">
              <form string="Manage your Employee's TimeOff">
                  <sheet>
                    <group>
                       <field name="employ_id" string="Employee-Name" />
                       <!-- <field name="only_oneday"/> -->
                       <field name="begin_date"/>
                       <field name="end_date"/>
                       <field name="day_hours"/>
                    </group>
                    <div>
                    <group>
                       <field name="requested_hours" readonly="1"/>
                    </group>
                    </div>
                  </sheet>
              </form>
          </field>
  </record>
  <record id="project_leaveoff_calendar_view" model="ir.ui.view">
    <field name="name">view.project.leaveoff.calendar</field>
    <field name="model">project.leaveoff</field>
    <field name="arch" type="xml">
        <calendar string="LeaveOff Calendar" date_start="begin_date" date_stop="end_date" color="employ_id">
            <field name="employ_id"  string="Employee"/>
            <field name="day_hours" string="Hours per day"/>
        </calendar>
    </field>
  </record>
  <!-- create action -->
  <!-- tree view -->
  <record id="project_leaveoff_tree_view" model="ir.ui.view">
          <field name="name">view.project.leaveoff.tree</field>
          <field name="model">project.leaveoff</field>
          <field name="arch" type="xml">
            <tree string="LeaveOff-Time">
              <field name="employ_id" string="Employee-Name"/>
              <field name="begin_date"/>
              <field name="end_date"/>
              <field name="day_hours"/>
              <field name="requested_hours"/>
            </tree>
          </field>
  </record>
  <menuitem  id="menu_main_leaveoff" name="LeaveOff" parent="project.menu_main_pm" action="action_for_leaveoff"/>
</odoo>

还有 Odoo 错误:

错误:Odoo 服务器错误

Traceback(最近一次调用最后一次):文件 “/usr/lib/python3/dist-packages/odoo/api.py”,第 745 行,在获取中 value = self._data[field][record._ids[0]] KeyError: 51

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次):文件 “/usr/lib/python3/dist-packages/odoo/fields.py”,第 1004 行,在 get 中 value = env.cache.get(record, self) 文件“/usr/lib/python3/dist-packages/odoo/api.py”,第 751 行,在 get raise CacheMiss(record, field) odoo.exceptions.CacheMiss: ('project.leaveoff(51,).requested_hours', None)

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次):文件 “/usr/lib/python3/dist-packages/odoo/models.py”,第 5101 行,在 确保一个 _id, = self._ids ValueError: 要解包的值太多(预期为 1)

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次):文件 “/usr/lib/python3/dist-packages/odoo/http.py”,第 624 行,在 _handle_exception 返回 super(JsonRequest, self)._handle_exception(exception) 文件“/usr/lib/python3/dist-packages/odoo/http.py”,第 310 行,在 _handle_exception raise pycompat.reraise(type(exception), exception, sys.exc_info()[2]) 文件 “/usr/lib/python3/dist-packages/odoo/tools/pycompat.py”,第 14 行,在 再加注 在调度中提高值文件“/usr/lib/python3/dist-packages/odoo/http.py”,第 669 行 结果 = self._call_function(**self.params) 文件“/usr/lib/python3/dist-packages/odoo/http.py”,第 350 行,在 _call_function 返回 checked_call(self.db, *args, **kwargs) 文件“/usr/lib/python3/dist-packages/odoo/service/model.py”,第 94 行,在 包装 返回 f(dbname, *args, **kwargs) 文件“/usr/lib/python3/dist-packages/odoo/http.py”,第 339 行,在 check_call 结果 = self.endpoint(*a, **kw) 文件“/usr/lib/python3/dist-packages/odoo/http.py”,第 915 行,在 call 中 返回 self.method(*args, **kw) 文件“/usr/lib/python3/dist-packages/odoo/http.py”,第 515 行,在 response_wrap 响应 = f(*args, **kw) 文件“/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py”, 第 1285 行,在 search_read 中 return self.do_search_read(model, fields, offset, limit, domain, sort) 文件 “/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py”, 第 1304 行,在 do_search_read 中 返回 Model.web_search_read(domain, fields, offset=offset, limit=limit, order=sort) 文件 “/usr/lib/python3/dist-packages/odoo/addons/web/models/models.py”, 第 39 行,在 web_search_read 中 记录 = self.search_read(域,字段,偏移量 = 偏移量,限制 = 限制,订单 = 订单)文件 “/usr/lib/python3/dist-packages/odoo/models.py”,第 4951 行,在 搜索阅读 结果 = records.read(fields) 文件“/usr/lib/python3/dist-packages/odoo/models.py”,第 2965 行,正在读取 vals[name] = convert(record[name], record, use_name_get) 文件“/usr/lib/python3/dist-packages/odoo/models.py”,第 5731 行,在 获取项目 return self._fields[key].get(self, type(self)) File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1028, in 得到 self.compute_value(recs) 文件“/usr/lib/python3/dist-packages/odoo/fields.py”,第 1113 行,在 计算值 records._compute_field_value(self) 文件“/usr/lib/python3/dist-packages/odoo/models.py”,第 4003 行,在 _compute_field_value getattr(self, field.compute)() 文件“/opt/Odoo/Custom_Addon/project_addon/models/leaveoff.py”,第 31 行,在 _compute_requested_hours start = self.begin_date 文件“/usr/lib/python3/dist-packages/odoo/fields.py”,第 988 行,在 get 中 record.ensure_one() 文件“/usr/lib/python3/dist-packages/odoo/models.py”,第 5104 行,在 确保一个 raise ValueError("Expected singleton: %s" % self) ValueError: Expected singleton: project.leaveoff(51, 52)

有人可以帮帮我吗?谢谢!

【问题讨论】:

【参考方案1】:

问题在于您的 compute 方法。

def _compute_requested_hours(self):
    for record in self:
        # Change here (Assigned before if the below condition not full-fill it still set the 0.0 value)
        record.requested_hours = 0.0
        if record.employ_id.id:
            start = record.begin_date
            end = record.end_date
            delta = end - start
            # Change here
            record.requested_hours = record.day_hours * (delta.days + 1)

【讨论】:

谢谢@Dipen Shah。我知道在循环中保存了旧值,这就是为什么我必须将值放回零的原因。不幸的是,我仍然遇到单例错误。 改进的代码,我忘记更改所有内线使用的循环。

以上是关于Odoo 13 错误:交换到树视图时需要单例的主要内容,如果未能解决你的问题,请参考以下文章

macOS 10.13 High Sierra odoo开发配置

odoo form视图 里面合计是怎么算出来的

Odoo - 添加新字段时出错:ValidateError 验证字段拱时发生错误:视图架构的 XML 无效

安装插件时出现 Odoo 错误:查看继承错误

隐藏创建和导出所有按钮,但在 Odoo13 树视图中显示导入按钮

odoo14和Odoo15的区别