在窗体上有拖曳按钮可以切换所有记录,第二个按钮可以清除所有完成的记录,但这不起作用,但是会产生错误 “ AttributeError:类型对象'todo.task'没有属性'do_toggle_done'” 属性造成什么损坏以及如何为两个按钮添加
两个按钮均出现错误
(# -*- coding: utf-8 -*-
from odoo import models, fields, api
class TodoTask(models.Model):
_name = 'todo.task'
_description = 'To-do Task'
name = fields.Char('Description', required=True)
is_done = fields.Boolean('Done?')
active = fields.Boolean('Active?', default=True)
@api.multi
def do_toggle_done(self):
for task in self:
task.is_done = not task.is_done
return True
@api.model
def do_clear_done(self):
dones = self.search([('is_done', '=', True)])
dones.write({'active': False})
return True
)
THIS IS THE XML CODE
<?xml version="1.0"?>
<odoo>
<!-- To-Do Task Form view -->
<record id="view_form_todo_task" model="ir.ui.view">
<field name="name">To-do Task Form</field>
<field name="model">todo.task</field>
<field name="arch" type="xml">
<form>
<header>
<button name="do_toggle_done" type="object"
string="Toggle Done" class="oe_highlight"/>
<button name="do_clear_done" type="object"
string="Clear All Done" />
</header>
<sheet>
<group name="group_top">
<group name="group_left">
<field name="name"/>
</group>
<group name="group_right">
<field name="is_done"/>
<field name="active" readonly="1"/>
</group>
</group>
</sheet>
</form>
</field>
</record>
<!-- To-Do Task List view -->
<record id="view_tree_todo_task" model="ir.ui.view">
<field name="name">To-do Task Tree</field>
<field name="model">todo.task</field>
<field name="arch" type="xml">
<tree colors="decoration-muted: is_done==True">
<field name="name"/>
<field name="is_done"/>
</tree>
</field>
</record>
<!-- To-Do Task Search view -->
<!--<record id="view_filter_todo_task" model="ir.ui.view">-->
<!--<field name="name">To-do Task Filter</field>-->
<!--<field name="model">todo.task</field>-->
<!--<field name="arch" type="xml">-->
<!--<search>-->
<!--<field name="name"/>-->
<!--<filter string="Not Done" domain="[('is_done','=',False)]"/>-->
<!--<filter string="Done" domain="[('is_done','!=',False)]"/>-->
<!--</search>-->
<!--</field>-->
<!--</record>-->
</odoo>
这是错误消息
Odoo Server Error
Traceback (most recent call last):
File "/odoo/odoo-server/odoo/http.py", line 654, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/odoo/odoo-server/odoo/http.py", line 312, in _handle_exception
raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
File "/odoo/odoo-server/odoo/tools/pycompat.py", line 87, in reraise
raise value
File "/odoo/odoo-server/odoo/http.py", line 696, in dispatch
result = self._call_function(**self.params)
File "/odoo/odoo-server/odoo/http.py", line 344, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/odoo/odoo-server/odoo/service/model.py", line 97, in wrapper
return f(dbname, *args, **kwargs)
File "/odoo/odoo-server/odoo/http.py", line 337, in checked_call
result = self.endpoint(*a, **kw)
File "/odoo/odoo-server/odoo/http.py", line 939, in __call__
return self.method(*args, **kw)
File "/odoo/odoo-server/odoo/http.py", line 517, in response_wrap
response = f(*args, **kw)
File "/odoo/odoo-server/addons/web/controllers/main.py", line 966, in call_button
action = self._call_kw(model, method, args, {})
File "/odoo/odoo-server/addons/web/controllers/main.py", line 954, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/odoo/odoo-server/odoo/api.py", line 742, in call_kw
method = getattr(type(model), name)
AttributeError: type object 'todo.task' has no attribute 'do_toggle_done'
答案 0 :(得分:0)
按钮调用与当前活动的model
和recordset
直接相关。您需要使用@api.multi
和object
类型button
一起使用。使用@api.multi
装饰器更改按钮方法。使用您的代码,其他一切似乎都很好。应该可以正常工作。
答案 1 :(得分:0)
尝试重新启动odoo并更新模块,它对我有用。