You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo跨插件One2Many关联报错:AttributeError问题求助

解决Odoo跨模块模型关联的AttributeError问题

问题背景

需要关联两个不同Odoo插件中的模型,已在manifest文件的依赖列表中添加其中一个插件,但出现报错。

相关代码

class Employee(models.Model):
    _name = 'prof.zaouia.employee'

  
    image = fields.Binary()
    name = fields.Char(string='Nom', required=True)
    prenom = fields.Char(string='Prénom', required=True)
    charge = fields.Integer(string='personnes à charge', required=True)
    situation = fields.Selection([('M', 'M'), ('C', 'C')], default='M')
    nombre_jours = fields.Integer(string='nombre de jours travaillés', default=26)
    periode = fields.Date(string='salaire au', default=fields.Date.today())
    nationality = fields.Many2one(string='Nationalité', comodel_name='prof.zaouia.nationality')
    salaire_brutm = fields.Float(string='Salaire brut mensuel')
    salaire_bdu = fields.Float(string='Salaire brut dû', compute='_compute_salaire_bdu', store=True)
    specializations = fields.Many2one(string='Métier', comodel_name='prof.zaouia.specialization')
    reservation_id = fields.Many2one(comodel_name="roombooking", string="responsable for")


class RoomBooking(models.Model):
    _name = "roombooking"
    booking_id = fields.Integer("booking_ID")
    checkInDate = fields.Date("check_in")
    checkoutDate = fields.Date("check_out")
    payement_status = fields.Boolean("payement_status")
    totale_amount = fields.Float("total")
    rooms = fields.One2many("room", "roombooking_id", string="rooms")
    client_id = fields.Many2one("client", string="client")
    responsables = fields.One2many(comodel_name = "prof.zaouia.employee", inverse_name = "reservation_id", String="responsables")

报错信息

RPC_ERROR
Odoo Server Error
Traceback (most recent call last):
  File "C:\Users\hossa\odoo\odoo\http.py", line 1584, in _serve_db
    return service_model.retrying(self._serve_ir_http, self.env)
  File "C:\Users\hossa\odoo\odoo\service\model.py", line 134, in retrying
    result = func()
  File "C:\Users\hossa\odoo\odoo\http.py", line 1611, in _serve_ir_http
    response = self.dispatcher.dispatch(rule.endpoint, args)
  File "C:\Users\hossa\odoo\odoo\http.py", line 1808, in dispatch
    result = self.request.registry['ir.http']._dispatch(endpoint)
  File "C:\Users\hossa\odoo\odoo\addons\base\models\ir_http.py", line 149, in _dispatch
    result = endpoint(**request.params)
  File "C:\Users\hossa\odoo\odoo\http.py", line 699, in route_wrapper
    result = endpoint(self, *args, **params_ok)
  File "c:\users\hossa\odoo\addons\web\controllers\dataset.py", line 42, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "c:\users\hossa\odoo\addons\web\controllers\dataset.py", line 33, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "C:\Users\hossa\odoo\odoo\api.py", line 461, in call_kw
    result = _call_kw_multi(method, model, args, kwargs)
  File "C:\Users\hossa\odoo\odoo\api.py", line 448, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "C:\Users\hossa\odoo\odoo\models.py", line 6475, in onchange
    record._update_cache(changed_values, validate=False)
  File "C:\Users\hossa\odoo\odoo\models.py", line 5274, in _update_cache
    value = field.convert_to_cache(value, self, validate)
  File "C:\Users\hossa\odoo\odoo\fields.py", line 2965, in convert_to_cache
    id_ = comodel.new(value, origin=origin).id
  File "C:\Users\hossa\odoo\odoo\models.py", line 5665, in new
    origin = origin.id
AttributeError: '_unknown' object has no attribute 'id'

The above server error caused the following client error:
null

解决方案

  • 修正字段参数拼写错误:RoomBooking类中responsables字段的String参数首字母大写,Odoo字段参数需为小写string,修改后代码:
    responsables = fields.One2many(comodel_name = "prof.zaouia.employee", inverse_name = "reservation_id", string="responsables")
    
  • 确认模型名称与模块依赖:
    • 确保roombooking模型确实存在于依赖模块中,且模型_name与代码中引用的名称完全一致(区分大小写)。
    • 检查模块依赖的双向性:若prof.zaouia.employee所在模块依赖roombooking模块,需在前者的__manifest__.py的depends列表中添加后者模块名;反之若roombooking模块需要引用prof.zaouia.employee,则需在后者的__manifest__.py中添加前者模块名,避免加载顺序问题。
  • 清除缓存并重启服务:修改代码或依赖后,重启Odoo服务,同时清除浏览器缓存和Odoo系统缓存(开发者模式下点击"清除缓存"按钮),确保新定义生效。
  • 验证关联字段完整性:确认Employee类中reservation_id的comodel_name准确指向roombooking模型,且RoomBooking的inverse_name与该字段名完全匹配。

内容的提问来源于stack exchange,提问作者Hossam Benhammou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 06:52:53