Odoo 自定义模块修改 crm.leads

Odoo custom module modifying crm.leads

我想将自定义模块中的一些字段(按照此处的教程:https://www.odoo.com/documentation/11.0/howtos/backend.html#model-inheritance)添加到潜在客户表单中,但我无法让它工作。

我总是收到以下错误:

Odoo Server Error

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/odoo/models.py", line 971, in _validate_fields
    check(self)
  File "/usr/lib/python3/dist-packages/odoo/addons/base/ir/ir_ui_view.py", line 321, in _check_xml
    self.postprocess_and_fields(view.model, view_doc, view.id)
  File "/usr/lib/python3/dist-packages/odoo/addons/base/ir/ir_ui_view.py", line 919, in postprocess_and_fields
    self.raise_view_error(message, view_id)
  File "/usr/lib/python3/dist-packages/odoo/addons/base/ir/ir_ui_view.py", line 489, in raise_view_error
    raise ValueError(message)
ValueError: Field `country` does not exist

Error context:
View `crm_case_form_view_leads_extended`
[view_id: 990, xml_id: n/a, model: crm.lead, parent_id: 442]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/odoo/tools/convert.py", line 741, in parse
    self._tags[rec.tag](rec, de, mode=mode)
  File "/usr/lib/python3/dist-packages/odoo/tools/convert.py", line 651, in _tag_record
    id = self.env(context=rec_context)['ir.model.data']._update(rec_model, self.module, res, rec_id or False, not self.isnoupdate(data_node), noupdate=self.isnoupdate(data_node), mode=self.mode)
  File "/usr/lib/python3/dist-packages/odoo/addons/base/ir/ir_model.py", line 1467, in _update
    record = record.create(values)
  File "/usr/lib/python3/dist-packages/odoo/addons/base/ir/ir_ui_view.py", line 384, in create
    return super(View, self).create(self._compute_defaults(values))
  File "/usr/lib/python3/dist-packages/odoo/models.py", line 3289, in create
    self._fields[key].determine_inverse(record)
  File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1078, in determine_inverse
    getattr(records, self.inverse)()
  File "/usr/lib/python3/dist-packages/odoo/addons/base/ir/ir_ui_view.py", line 253, in _inverse_arch
    view.write(data)
  File "/usr/lib/python3/dist-packages/odoo/addons/base/ir/ir_ui_view.py", line 400, in write
    return super(View, self).write(self._compute_defaults(vals))
  File "/usr/lib/python3/dist-packages/odoo/models.py", line 3011, in write
    self._write(old_vals)
  File "/usr/lib/python3/dist-packages/odoo/models.py", line 3164, in _write
    self._validate_fields(vals)
  File "/usr/lib/python3/dist-packages/odoo/models.py", line 975, in _validate_fields
    raise ValidationError("%s\n\n%s" % (_("Error while validating constraint"), tools.ustr(e)))
odoo.exceptions.ValidationError: ('Error while validating constraint\n\nField `country` does not exist\n\nError context:\nView `crm_case_form_view_leads_extended`\n[view_id: 990, xml_id: n/a, model: crm.lead, parent_id: 442]', None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 646, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 307, in _handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "/usr/lib/python3/dist-packages/odoo/tools/pycompat.py", line 87, in reraise
    raise value
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 683, in dispatch
    result = self._call_function(**self.params)
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 339, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/odoo/service/model.py", line 97, in wrapper
    return f(dbname, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 332, in checked_call
    result = self.endpoint(*a, **kw)
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 927, in __call__
    return self.method(*args, **kw)
  File "/usr/lib/python3/dist-packages/odoo/http.py", line 512, in response_wrap
    response = f(*args, **kw)
  File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 924, in call_button
    action = self._call_kw(model, method, args, {})
  File "/usr/lib/python3/dist-packages/odoo/addons/web/controllers/main.py", line 912, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "/usr/lib/python3/dist-packages/odoo/api.py", line 689, in call_kw
    return call_kw_multi(method, model, args, kwargs)
  File "/usr/lib/python3/dist-packages/odoo/api.py", line 680, in call_kw_multi
    result = method(recs, *args, **kwargs)
  File "<decorator-gen-39>", line 2, in button_immediate_install
  File "/usr/lib/python3/dist-packages/odoo/addons/base/module/module.py", line 70, in check_and_log
    return method(self, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/odoo/addons/base/module/module.py", line 437, in button_immediate_install
    return self._button_immediate_function(type(self).button_install)
  File "/usr/lib/python3/dist-packages/odoo/addons/base/module/module.py", line 530, in _button_immediate_function
    modules.registry.Registry.new(self._cr.dbname, update_module=True)
  File "/usr/lib/python3/dist-packages/odoo/modules/registry.py", line 84, in new
    odoo.modules.load_modules(registry._db, force_demo, status, update_module)
  File "/usr/lib/python3/dist-packages/odoo/modules/loading.py", line 343, in load_modules
    loaded_modules, update_module)
  File "/usr/lib/python3/dist-packages/odoo/modules/loading.py", line 242, in load_marked_modules
    loaded, processed = load_module_graph(cr, graph, progressdict, report=report, skip_modules=loaded_modules, perform_checks=perform_checks)
  File "/usr/lib/python3/dist-packages/odoo/modules/loading.py", line 156, in load_module_graph
    _load_data(cr, module_name, idref, mode, kind='data')
  File "/usr/lib/python3/dist-packages/odoo/modules/loading.py", line 94, in _load_data
    tools.convert_file(cr, module_name, filename, idref, mode, noupdate, kind, report)
  File "/usr/lib/python3/dist-packages/odoo/tools/convert.py", line 788, in convert_file
    convert_xml_import(cr, module, fp, idref, mode, noupdate, report)
  File "/usr/lib/python3/dist-packages/odoo/tools/convert.py", line 849, in convert_xml_import
    obj.parse(doc.getroot(), mode=mode)
  File "/usr/lib/python3/dist-packages/odoo/tools/convert.py", line 738, in parse
    self.parse(rec, mode)
  File "/usr/lib/python3/dist-packages/odoo/tools/convert.py", line 748, in parse
    exc_info[2]
  File "/usr/lib/python3/dist-packages/odoo/tools/pycompat.py", line 86, in reraise
    raise value.with_traceback(tb)
  File "/usr/lib/python3/dist-packages/odoo/tools/convert.py", line 741, in parse
    self._tags[rec.tag](rec, de, mode=mode)
  File "/usr/lib/python3/dist-packages/odoo/tools/convert.py", line 651, in _tag_record
    id = self.env(context=rec_context)['ir.model.data']._update(rec_model, self.module, res, rec_id or False, not self.isnoupdate(data_node), noupdate=self.isnoupdate(data_node), mode=self.mode)
  File "/usr/lib/python3/dist-packages/odoo/addons/base/ir/ir_model.py", line 1467, in _update
    record = record.create(values)
  File "/usr/lib/python3/dist-packages/odoo/addons/base/ir/ir_ui_view.py", line 384, in create
    return super(View, self).create(self._compute_defaults(values))
  File "/usr/lib/python3/dist-packages/odoo/models.py", line 3289, in create
    self._fields[key].determine_inverse(record)
  File "/usr/lib/python3/dist-packages/odoo/fields.py", line 1078, in determine_inverse
    getattr(records, self.inverse)()
  File "/usr/lib/python3/dist-packages/odoo/addons/base/ir/ir_ui_view.py", line 253, in _inverse_arch
    view.write(data)
  File "/usr/lib/python3/dist-packages/odoo/addons/base/ir/ir_ui_view.py", line 400, in write
    return super(View, self).write(self._compute_defaults(vals))
  File "/usr/lib/python3/dist-packages/odoo/models.py", line 3011, in write
    self._write(old_vals)
  File "/usr/lib/python3/dist-packages/odoo/models.py", line 3164, in _write
    self._validate_fields(vals)
  File "/usr/lib/python3/dist-packages/odoo/models.py", line 975, in _validate_fields
    raise ValidationError("%s\n\n%s" % (_("Error while validating constraint"), tools.ustr(e)))
odoo.tools.convert.ParseError: "Error while validating constraint

Field `country` does not exist

Error context:
View `crm_case_form_view_leads_extended`
[view_id: 990, xml_id: n/a, model: crm.lead, parent_id: 442]
None" while parsing /mnt/extra-addons/leads_events/views/views.xml:6, near
<record id="crm.crm_case_form_view_leads_extended" model="ir.ui.view">
      <field name="name">crm_case_form_view_leads_extended</field>
      <field name="model">crm.lead</field>
      <field name="inherit_id" ref="crm.crm_case_form_view_leads"/>
      <field name="arch" type="xml">

        <data><!-- Change title placeholder -->
        <xpath expr="//h1/field[@name='name']" position="replace">
          <field name="name" placeholder="Name"/>
        </xpath>

        <!-- Add new fields -->
        <xpath expr="//notebook" position="before">
            <group>
              <field name="city"/>
              <field name="country"/>
            </group>
        </xpath>
      </data></field>
    </record>

这是我的代码:

__manifest__.py

'data': [
    # 'security/ir.model.access.csv',
    'views/views.xml',

__init__.py

# -*- coding: utf-8 -*-

from . import controllers
from . import models

models.py

# -*- coding: utf-8 -*-

from odoo import models, fields, api

class Leads_extended(models.Model):
    _inherit = 'crm.lead'
    _name = 'crm.lead.events'

    city = fields.Char(String="City")
    country = fields.Many2one('res.country', string="Country")

views.xml

<odoo>
    <data>

        <!-- explicit list view definition -->    
        <record id="crm.crm_case_form_view_leads_extended" model="ir.ui.view">
            <field name="name">crm_case_form_view_leads_extended</field>
            <field name="model">crm.lead</field>
            <field name="inherit_id" ref="crm.crm_case_form_view_leads"/>
            <field name="arch" type="xml">

                <!-- Change title placeholder -->
                <xpath expr="//h1/field[@name='name']" position="replace">
                    <field name="name" placeholder="Name" />
                </xpath>

                <!-- Add new fields -->
                <xpath expr="//notebook" position="before">
                    <group>
                        <field name="city"/>
                        <field name="country"/>
                    </group>
                </xpath>
            </field>
        </record>
    </data>
</odoo>

我不知道我的代码有什么问题...有线索吗?

谢谢!

当您将模块名称设置为 _name = 'crm.lead.events' 时,您会创建一个新的 table。

在您的 xml 上,您仍在引用 crm.lead。所以,删除行

_name = 'crm.lead.events' 

它应该可以工作。