Odoo - 错误上下文

Odoo - Error Context

我正在尝试使用另一个模块修改模块的行为,但我不断收到相同的错误消息。

Traceback (most recent call last):
  File "/home/carlos/dbTest/codigo/OCB/openerp/http.py", line 537, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/home/carlos/dbTest/codigo/OCB/openerp/http.py", line 574, in dispatch
    result = self._call_function(**self.params)
  File "/home/carlos/dbTest/codigo/OCB/openerp/http.py", line 310, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/home/carlos/dbTest/codigo/OCB/openerp/service/model.py", line 118, in wrapper
    return f(dbname, *args, **kwargs)
  File "/home/carlos/dbTest/codigo/OCB/openerp/http.py", line 307, in checked_call
    return self.endpoint(*a, **kw)
  File "/home/carlos/dbTest/codigo/OCB/openerp/http.py", line 803, in __call__
    return self.method(*args, **kw)
  File "/home/carlos/dbTest/codigo/OCB/openerp/http.py", line 403, in response_wrap
    response = f(*args, **kw)
  File "/home/carlos/dbTest/codigo/OCB/addons/web/controllers/main.py", line 944, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "/home/carlos/dbTest/codigo/OCB/addons/web/controllers/main.py", line 936, in _call_kw
    return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
  File "/home/carlos/dbTest/codigo/OCB/openerp/api.py", line 268, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/carlos/dbTest/codigo/OCB/addons/mail/mail_thread.py", line 348, in fields_view_get
    res = super(mail_thread, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
  File "/home/carlos/dbTest/codigo/OCB/openerp/api.py", line 268, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/carlos/dbTest/codigo/OCB/openerp/models.py", line 1545, in fields_view_get
    xarch, xfields = View.postprocess_and_fields(cr, uid, self._name, etree.fromstring(result['arch']), view_id, context=ctx)
  File "/home/carlos/dbTest/codigo/OCB/openerp/api.py", line 268, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/carlos/dbTest/codigo/OCB/openerp/addons/base/ir/ir_ui_view.py", line 837, in postprocess_and_fields
    self.raise_view_error(cr, user, message, view_id, context)
  File "/home/carlos/dbTest/codigo/OCB/openerp/api.py", line 268, in wrapper
    return old_api(self, *args, **kwargs)
  File "/home/carlos/dbTest/codigo/OCB/openerp/addons/base/ir/ir_ui_view.py", line 360, in raise_view_error
    raise AttributeError(message)
AttributeError: Field `code` does not exist

Error context:
View `CRM - Claims Tree`
[view_id: 1015, xml_id: crm_claim.crm_case_claims_tree_view, model: crm.claim, parent_id: n/a]

这是我要修改的模块。该模块修改了 crm.claim 的行为,并且工作正常

    from openerp import models, fields, api


class CrmClaim(models.Model):
    _name = "crm.claim.code"
    _inherit = "crm.claim"

    code = fields.Char(
        string='Claim Number', required=True, default="/", readonly=True)

    _sql_constraints = [
        ('crm_claim_unique_code', 'UNIQUE (code)',
         'The code must be unique!'),
    ]

    @api.model
    def create(self, vals):
        if vals.get('code', '/') == '/':
            vals['code'] = self.env['ir.sequence'].get('crm.claim')
            #vals['stage_id'] = '1' #Set stage to "New" automatically
        return super(CrmClaim, self).create(vals)

这是上一个模块的XML视图

<?xml version="1.0" encoding="UTF-8"?>
<openerp>
    <data>
        <record model="ir.ui.view" id="crm_claim_add_seq_form_view_inh">
            <field name="name">crm.claim.add.seq.form.view.inh</field>
            <field name="model">crm.claim</field>
            <field name="inherit_id" ref="crm_claim.crm_case_claims_form_view" />
            <field name="arch" type="xml">
                <field name="name" position="before">
                    <field name="code" />
                </field>
            </field>
        </record>

        <record model="ir.ui.view" id="crm_claim_add_seq_tree_view_inh">
            <field name="name">crm.claim.add.seq.tree.view.inh</field>
            <field name="model">crm.claim</field>
            <field name="inherit_id" ref="crm_claim.crm_case_claims_tree_view" />
            <field name="arch" type="xml">
                <field name="name" position="before">
                    <field name="code" />
                </field>
            </field>
        </record>

        <record model="ir.ui.view" id="view_crm_case_claims_filter_inh_claimseq">
            <field name="name">view.crm.case.claims.filter.inh.claimseq</field>
            <field name="model">crm.claim</field>
            <field name="inherit_id" ref="crm_claim.view_crm_case_claims_filter" />
            <field name="arch" type="xml">
                <field name="name" position="attributes">
                    <attribute name="filter_domain">['|', ('name', 'ilike', self), ('code', 'ilike', self)]</attribute>
                </field>
            </field>
        </record>
    </data>
</openerp>

这是我的模块,我只是想在创建声明后强制将阶段设置为 1(新)。

    from openerp import models, fields, api


class CrmClaim(models.Model):
    _name = "crm.claim.code"
    _inherit = "crm.claim"

    code = fields.Char(
        string='Claim Number', required=True, default="/", readonly=True)

    _sql_constraints = [
        ('crm_claim_unique_code', 'UNIQUE (code)',
         'The code must be unique!'),
    ]

    @api.model
    def create(self, vals):
        if vals.get('code', '/') == '/':
            vals['code'] = self.env['ir.sequence'].get('crm.claim')
            #vals['stage_id'] = '1' #Set stage to "New" automatically
        return super(CrmClaim, self).create(vals)

这是我的目录结构

odoo
|[...]
|Upgrade        (This is mine)
|  |crm_claim_create
|       |crmClaimCreate.py
|
|crm            (This is the one I'm trying to modify)
|  |crm_claim_code
|       |models
|       |  |crm_claim.py
|       |views
|          |crm_claim_view.xml
|OCB            (Odoo's base one)
   |addons
        |crm_claim
           |crm_claim.py 

知道发生了什么吗?

谢谢

您的 python 代码有误。如果您只想继承 crm.claim 模型,则不应将 _name 赋予模型 - 当调用 _name 和 _inherit 时,将创建新模型,该模型继承自给定模型 - 删除行 _name = "crm.claim.code" 会有帮助。

odoo 无法完善您的领域,这可能有很多原因

  • 添加该字段后没有重启服务器
  • 您没有在 init.py 文件
  • 中导入包含此字段的文件
  • 此 python 文件中有错误

但在你的情况下你这样做了

  • 首先定义“crm.claim.code”对象
  • 您继承了“crm.claim”对象
  • 定义真正在“crm.claim.code”对象中定义的代码字段,而您想在发生错误的“crm.claim”对象视图中显示它