单击自定义模块添加的新按钮时出现 AttributeError

AttributeError when clicking on new button added by custom module

我试图通过自定义模块在 res.partner 表单视图 (base.view_partner_form) 上添加一个新按钮以打开一个新的浏览器选项卡,但我收到以下错误:

    Traceback (most recent call last):
  File "/opt/odoo/odoo-server/openerp/http.py", line 540, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/opt/odoo/odoo-server/openerp/http.py", line 577, in dispatch
    result = self._call_function(**self.params)
  File "/opt/odoo/odoo-server/openerp/http.py", line 313, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/opt/odoo/odoo-server/openerp/service/model.py", line 118, in wrapper
    return f(dbname, *args, **kwargs)
  File "/opt/odoo/odoo-server/openerp/http.py", line 310, in checked_call
    return self.endpoint(*a, **kw)
  File "/opt/odoo/odoo-server/openerp/http.py", line 806, in __call__
    return self.method(*args, **kw)
  File "/opt/odoo/odoo-server/openerp/http.py", line 406, in response_wrap
    response = f(*args, **kw)
  File "/opt/odoo/odoo-server/addons/web/controllers/main.py", line 948, in call_button
    action = self._call_kw(model, method, args, {})
  File "/opt/odoo/odoo-server/addons/web/controllers/main.py", line 936, in _call_kw
    return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
AttributeError: 'res.partner' object has no attribute 'g_search'

按钮出现了,但我被那个错误卡住了。这些是我的文件内容:

mymodule.py

import openerp
from openerp.osv import fields, osv, orm
from openerp import models

class cant_neg_gs(osv.osv):
    _inherit = 'res.partner'

    @api.multi
    def g_search(self):
        res = {
            'type': 'ir.actions.act_url',
            'url': 'http://www.myurl.com',
            'target': 'new',
        } 
    return res

mymodule.xml

<?xml version="1.0"?>
<openerp>
    <data>
        <record id="gs_cant_neg_view" model="ir.ui.view">
            <field name="name">res.partner.cant_neg_gs.inherit</field>
            <field name="model">res.partner</field>
            <field name="inherit_id" ref="base.view_partner_form"/>
            <field name="arch" type="xml">
                <xpath expr="//field[@name='title']" position="after">
                    <button string="Buscar empresa" type="object" name="g_search" class="oe_highlight"/>
                </xpath>
            </field>
        </record>
    </data>
</openerp>

__init__.py

import cant_neg_gs

__openerp__.py

{
    'name': 'My Module,
    'version': '1.0',
    'category': 'Uncategorized',
    'summary': 'Summary',
    'sequence': 2,
    'description': """

Info
====================================================
More info

Things :
-------------------------------
* Button
""",
    'author': 'Name',
    'website': 'https://example.com',
    'depends': ['base',],
    'data': [
        'cant_neg_gs.xml',
    ],
    'installable': True,
    'application': True,
    'auto_install': False,
}

非常感谢您的帮助!

此致。

您有 import import cant_neg_gs class 名称,但您必须导入文件。名称不是 class 名称 例如:import mymodule.

你的代码还有一个问题,你可以按照下面的代码进行操作。

from openerp import models,fields,api
class res_partner(models.Model):
    _inherit="res.partner"

    @api.multi
    def g_search(self):
        res = {
            'type': 'ir.actions.act_url',
            'url': 'http://www.myurl.com',
            'target': 'new',
        } 
        return res

在你的文件中 api 没有导入,你已经使用了 @api.multi 所以你必须导入 api.

这可能对您有所帮助。