Odoo, Python: TypeError: info() got multiple values for keyword argument 'title'

Odoo, Python: TypeError: info() got multiple values for keyword argument 'title'

我正在尝试使用这个 Odoo 插件:

https://www.odoo.com/apps/modules/8.0/warning_box/

https://github.com/ingadhoc/odoo-addons/tree/8.0/warning_box

显示一些消息。

安装页面显示:

usage return self.pool.get('warning_box').info(cr, uid, title='The title', message='the message')

由于代码是用V8.0风格写的,我认为这是错误的。无论如何我都试过了,它给出了关于 cr 和 uid 的错误。

然后我这样试了一下:

self.env['warning_box'].info(self, title='The title', message='the message')

这给了我这个错误:

TypeError: info() got multiple values for keyword argument 'title'

这是 Odoo 插件的 python 代码:

WARNING_TYPES = [('warning', 'Warning'), ('info', 'Information'), ('error', 'Error')]

class warning_box(models.TransientModel):
    _name = 'warning_box'
    _description = 'warning_box'
    _req_name = 'title'

    type = fields.Selection(WARNING_TYPES, string='Type', readonly=True)
    title = fields.Char(string="Title", size=100, readonly=True)
    message = fields.Text(string="Message", readonly=True)

    @api.multi
    def message_action(self):
        self.ensure_one
        message_type = [t[1]for t in WARNING_TYPES if self.type == t[0]][0]
        res = {
            'name': '%s: %s' % (_(message_type), _(self.title)),
            'view_type': 'form',
            'view_mode': 'form',
            'view_id': self.env['ir.model.data'].xmlid_to_res_id(
                'warning_box.warning_box_form'),
            'res_model': 'warning_box',
            'domain': [],
            'context': self._context,
            'type': 'ir.actions.act_window',
            'target': 'new',
            'res_id': self.id
        }
        return res

    @api.model
    def warning(self, title, message):
        record = self.create({'title': title, 'message': message, 'type': 'warning'})
        return record.message_action()

    @api.model
    def info(self, title, message):
        record = self.create({'title': title, 'message': message, 'type': 'info'})
        return record.message_action()

    @api.model
    def error(self, title, message):
        record = self.create({'title': title, 'message': message, 'type': 'error'})
        return record.message_action()

我一直在查找错误,发现了这2条信息:

class method generates "TypeError: ... got multiple values for keyword argument ..."

TypeError: create() got multiple values for keyword argument 'context'

我试图理解它并将其应用到我的情况中,但我无法让它工作... 谁能告诉我这段代码有什么问题吗?

为 Forvas 编辑:

我现在这样调用函数:

return self.env['warning_box'].error(title='The title', message='the message')

上面的代码没有给出任何错误。

现在我已经像你说的那样改变了 def message_action。 为此:

form_view_id = self.env.ref(
        'your_module_name.your_form_view_xml_id').id

我用过:

form_view_id = self.env.ref(
    'warning_box_git.warning_box_form').id

请确认一下,您指的是模块名称还是型号名称? 我的模型 (class) 是 warning_box,我的模块名称是 warning_box_git(模块文件夹的名称)。我做对了吗?

无论哪种方式,我都会不断收到此错误:

AttributeError: 'warning_box' object has no attribute 'error'

这是我的 XML:

<openerp> 
    <data> 
        <record id="warning_box_form" model="ir.ui.view">
             <field name="name">warning_box.form</field> 
             <field name="model">warning_box</field>
             <field eval="20" name="priority"/> 
             <field name="arch" type="xml"> 
                 <form string="Warning">
                    <field name="message" nolabel="1"/> 
                    <footer>
                        <button string="OK" class="oe_highlight" special="cancel"/>
                    </footer> 
                 </form> 
            </field>
        </record>

        <record model="ir.actions.act_window" id="action_warning_box">
            <field name="name">Warning Box</field>
            <field name="res_model">warning_box</field>
            <field name="view_type">form</field>
            <field name="view_mode">form</field>
            <field name="view_id" ref="warning_box_form" />
            <field name="target">new</field>
        </record>
    </data>
</openerp>

你知道如何解决这个错误吗?

为 Forvas 编辑 2: 我犯了一个愚蠢的缩进错误。该错误现在消失了。 但是,它仍然没有显示任何弹出窗口?

您不能将 self 作为参数传递。

self.env['warning_box'].info(title='The title', message='the message')

当您从 self 调用模型时,您 "pass" 它。

编辑

尝试下一个代码:

@api.multi
def message_action(self):
    self.ensure_one()
    message_type = [t[1]for t in WARNING_TYPES if self.type == t[0]][0]
    form_view_id = self.env.ref(
        'your_module_name.your_form_view_xml_id').id
    return {
        'name': '%s: %s' % (_(message_type), _(self.title)),
        'view_type': 'form',
        'view_mode': 'form',
        'views': [(form_view_id, 'form'), ],
        'res_model': 'warning_box',
        'context': self._context,
        'type': 'ir.actions.act_window',
        'target': 'new',
        'flags': {'action_buttons': True},
    }