AttributeError: '_unknown' object has no attribute 'id' - in Odoo 15

AttributeError: '_unknown' object has no attribute 'id' - in Odoo 15

我添加了一个自定义字段res.patner,这个字段是Char数据类型,现在我想通过下面的代码将它添加到account.move上。

class InvoicingMove(models.Model):
    _inherit = 'account.move'
    
    patient_medical_aid_number_id = fields.Many2one('partner_id.member_medical_aid_number', string='Patient Medical Aid Number', readonly=True)

但是当我尝试开具发票时,我得到:

Traceback (most recent call last):
  File "C:\Program Files\Odoo\server\odoo\addons\base\models\ir_http.py", line 237, in _dispatch
    result = request.dispatch()
  File "C:\Program Files\Odoo\server\odoo\http.py", line 688, in dispatch
    result = self._call_function(**self.params)
  File "C:\Program Files\Odoo\server\odoo\http.py", line 360, in _call_function
    return checked_call(self.db, *args, **kwargs)
  File "C:\Program Files\Odoo\server\odoo\service\model.py", line 94, in wrapper
    return f(dbname, *args, **kwargs)
  File "C:\Program Files\Odoo\server\odoo\http.py", line 349, in checked_call
    result = self.endpoint(*a, **kw)
  File "C:\Program Files\Odoo\server\odoo\http.py", line 917, in __call__
    return self.method(*args, **kw)
  File "C:\Program Files\Odoo\server\odoo\http.py", line 536, in response_wrap
    response = f(*args, **kw)
  File "C:\Program Files\Odoo\server\odoo\addons\web\controllers\main.py", line 1348, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "C:\Program Files\Odoo\server\odoo\addons\web\controllers\main.py", line 1340, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "C:\Program Files\Odoo\server\odoo\api.py", line 464, in call_kw
    result = _call_kw_multi(method, model, args, kwargs)
  File "C:\Program Files\Odoo\server\odoo\api.py", line 451, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "C:\Program Files\Odoo\server\odoo\models.py", line 3227, in read
    return self._read_format(fnames=fields, load=load)
  File "C:\Program Files\Odoo\server\odoo\models.py", line 3247, in _read_format
    vals[name] = convert(record[name], record, use_name_get)
  File "C:\Program Files\Odoo\server\odoo\fields.py", line 2813, in convert_to_read
    return value.id
Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Program Files\Odoo\server\odoo\http.py", line 644, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "C:\Program Files\Odoo\server\odoo\http.py", line 302, in _handle_exception
    raise exception.with_traceback(None) from new_cause
AttributeError: '_unknown' object has no attribute 'id'

第一个参数 (partner_id.member_medical_aid_number) 将用作 comodel_name 并且当 Odoo 将尝试 set up the field 时,它将检查是否加载了 commodel 名称,如果没有加载它将其设置为 _unknown

您应该会在日志中看到以下警告:

Field account.move.patient_medical_aid_number_id with unknown comodel_name 'partner_id.member_medical_aid_number'

如果要使用相关字段,请设置 related 属性并使用相同的字段类型

示例:

patient_medical_aid_number_id = fields.Char(related='partner_id.member_medical_aid_number', string='Patient Medical Aid Number', readonly=True)