-1

表单有两个按钮来切换所有记录已完成,第二个按钮用于清除所有已完成的记录,但它不起作用但会生成错误“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'
4

2 回答 2

1

尝试重新启动 odoo 并更新模块,它对我有用。

于 2019-04-21T17:51:46.717 回答
0

按钮调用与当前活动model和直接相关recordset。您需要用于@api.multi使用objecttype button@api.multi用装饰器改变你的按钮方法。您的代码似乎一切都很好。应该可以完美运行。

于 2019-01-21T18:10:30.917 回答