【问题标题】:Odoo, Python: TypeError: info() got multiple values for keyword argument 'title'Odoo,Python:TypeError:info() 为关键字参数“标题”获取了多个值
【发布时间】:2015-12-30 14:38:08
【问题描述】:

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

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

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

显示一些消息。

安装页面显示:

用法 return self.pool.get('warning_box').info(cr, uid, title='The 标题',消息='消息')

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

然后我这样尝试:

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

这给了我这个错误:

TypeError: info() 为关键字参数 '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()

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

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

只是为了确定一下,您是指模块名称还是型号名称? 我的模型(类)是warning_box,我的模块名称是warning_box_git(模块文件夹的名称)。我这样做对吗?

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

AttributeError: 'warning_box' 对象没有属性 '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: 我犯了一个愚蠢的缩进错误。现在那个错误已经消失了。 但是,它仍然没有显示任何弹出窗口?

【问题讨论】:

    标签: xml python-2.7 typeerror odoo odoo-8


    【解决方案1】:

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

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

    当您从self 调用模型时,您“通过”它。

    编辑

    尝试下一个代码:

    @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},
        }
    

    【讨论】:

    • 但是仍然没有弹出窗口。你也知道为什么会这样吗?
    • 我已经编辑了我的答案@RobbeM,看看我想打开一个弹出窗口时我做了什么(显然用你的数据替换 your_module_name.your_form_view_xml_id) .
    • 感谢您的努力。但是我现在遇到了一个新错误...我已经用详细信息编辑了我的 OP。
    • 我认为问题一定出在装饰器上。您从具有@api.model 的其他方法调用message_action,尝试将其删除并改写@api.multi,或@api.one
    猜你喜欢
    • 1970-01-01
    • 2013-08-04
    • 2016-06-24
    • 2015-05-18
    • 1970-01-01
    • 2013-03-09
    • 2016-04-25
    • 2012-11-12
    • 1970-01-01
    相关资源
    最近更新 更多