odoo TypeError: Mixing Apples and Oranges on inherited model

odoo TypeError: Mixing Apples and Oranges on inherited model

我有 odoo 12 模块合同管理 - 经常性”,我正在编写一个模块,该模块创建一个名为“租约”的模型,该模型使用 _inherit 从 account.analytic.account 继承。表单视图继承自 contract.account_analytic_account_sale_form。它工作得很好,除了当我尝试从发票行添加产品时,出现错误

TypeError: Mixing apples and oranges: account.analytic.account(<odoo.models.NewId object at 0x07736650>,) & pmtest.leases(<odoo.models.NewId object at 0x07736650>,)

型号是:

class leases(models.Model):
    _name = 'pmtest.leases'
    _description = 'Leases - inherit from Contracts'
    _inherit = 'account.analytic.account'
    lease

姓名=fields.Char()

观点是

<record id="pmtest_leases_form" model="ir.ui.view">
    <field name="name">pmtest.leases.form</field>
    <field name="model">pmtest.leases</field>
    <field name="inherit_id" ref="contract.account_analytic_account_sale_form"/>
    <field name="mode">primary</field>
    <field name="priority" eval="20"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='partner_id']" position="after">
                <field name="leasename" string="Lease Name"/>
        </xpath>
    </field>
</record>

完整堆栈跟踪如下:

Odoo Server Error
Traceback (most recent call last):
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 654, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 312, in _handle_exception
    raise pycompat.reraise(type(exception), exception, sys.exc_info()[2])
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\tools\pycompat.py", line 87, in reraise
    raise value
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 696, in dispatch
    result = self._call_function(**self.params)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 344, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\service\model.py", line 97, in wrapper
    return f(dbname, *args, **kwargs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 337, in checked_call
    result = self.endpoint(*a, **kw)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 939, in __call__
    return self.method(*args, **kw)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\http.py", line 517, in response_wrap
    response = f(*args, **kw)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\web\controllers\main.py", line 962, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "c:\program files (x86)\odoo 12.0\server\odoo\addons\web\controllers\main.py", line 954, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 749, in call_kw
    return _call_kw_multi(method, model, args, kwargs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\api.py", line 736, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 5455, in onchange
    record[name] = value
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 5097, in __setitem__
    return self._fields[key].__set__(self, value)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\fields.py", line 1002, in __set__
    spec = self.modified_draft(record)
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\fields.py", line 1147, in modified_draft
    lambda rec: rec if path == 'id' else rec._mapped_cache(path) & records
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 4866, in filtered
    return self.browse([rec.id for rec in self if func(rec)])
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 4866, in <listcomp>
    return self.browse([rec.id for rec in self if func(rec)])
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\fields.py", line 1147, in <lambda>
    lambda rec: rec if path == 'id' else rec._mapped_cache(path) & records
  File "C:\Program Files (x86)\Odoo 12.0\server\odoo\models.py", line 5002, in __and__
    raise TypeError("Mixing apples and oranges: %s & %s" % (self, other))
TypeError: Mixing apples and oranges: account.analytic.account(<odoo.models.NewId object at 0x04F49BB0>,) & pmtest.leases(<odoo.models.NewId object at 0x04F49BB0>,)

我认为问题出在属性 _name.

尝试删除 _name 属性。

区别在于,如果您添加_name,您将在数据库中创建一个新的table。如果您只添加一个 _inherit 属性,您创建的字段 将被添加到 table 中,如 _inherit 属性。

如果您看到,错误提示您无法将 'account.analytic.account' 模型与 'pmtest.leases' 模型进行比较,因为 您使用 _name 定义的是不同的模型.