Odoo 13 Error: Expected singleton when swapping to tree view
Odoo 13 Error: Expected singleton when swapping to tree view
此错误 returns 2 条记录用于一个只能容纳 1 条记录的字段。当我想从日历视图切换到树视图时会发生这种情况。我假设这是因为 compute
函数,但我不明白我的代码中的错误在哪里。我为 2 个不同的模型计算字段:
class 休假:
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 错误:
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: 51
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: ('project.leaveoff(51,).requested_hours', 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 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/leaveoff.py", line 31, in
_compute_requested_hours
start = self.begin_date 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: project.leaveoff(51, 52)
有人可以帮帮我吗?谢谢!
问题出在您的 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)
此错误 returns 2 条记录用于一个只能容纳 1 条记录的字段。当我想从日历视图切换到树视图时会发生这种情况。我假设这是因为 compute
函数,但我不明白我的代码中的错误在哪里。我为 2 个不同的模型计算字段:
class 休假:
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 错误:
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: 51
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: ('project.leaveoff(51,).requested_hours', 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 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/leaveoff.py", line 31, in _compute_requested_hours start = self.begin_date 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: project.leaveoff(51, 52)
有人可以帮帮我吗?谢谢!
问题出在您的 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)