Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
1785 Lượt xem

Hello everyone,

​I have been trying to create a custom module in Odoo. What I want to do is add some additional fields for res.company​ module. I also want res.partner​ module as contact details section for res.company​ with OneToMany relation. So far I have inherited res.company module for my custom module and created a One2many field with co-model as res.partner and inverse field as parent_id. I have also inherited xml form views of the company to include the custom fields that I have added. Here is the code for model and views.

Model class:

class ResCompany(models.Model):
​_inherit = 'res.company'
​contact_ids = fields.One2many('res.partner', 'parent_id', string='Contacts', domain=[('is_company', '=', False)])
​comp_industry_type = fields.Many2one('res.partner.industry', string='Industry')

class ResPartner(models.Model):
​_inherit = 'res.partner'
​parent_id = fields.Many2one('res.company', string='Related Company', index=True)
​is_company = fields.Boolean(string='Is a Company', default=False)

View:


res.company.form.inherit
res.company



1









































Records are being displayed in Contact Details tab as expected. But when I click on Add a line​ button I get the following error:

Traceback (most recent call last):
  File "D:\odoo-17.0\odoo\http.py", line 1765, in _serve_db
    return service_model.retrying(self._serve_ir_http, self.env)
  File "D:\odoo-17.0\odoo\service\model.py", line 133, in retrying
    result = func()
  File "D:\odoo-17.0\odoo\http.py", line 1792, in _serve_ir_http
    response = self.dispatcher.dispatch(rule.endpoint, args)
  File "D:\odoo-17.0\odoo\http.py", line 1996, in dispatch
    result = self.request.registry['ir.http']._dispatch(endpoint)
  File "D:\odoo-17.0\odoo\addons\base\models\ir_http.py", line 222, in _dispatch
    result = endpoint(**request.params)
  File "D:\odoo-17.0\odoo\http.py", line 722, in route_wrapper
    result = endpoint(self, *args, **params_ok)
  File "d:\odoo-17.0\addons\web\controllers\dataset.py", line 24, in call_kw
    return self._call_kw(model, method, args, kwargs)
  File "d:\odoo-17.0\addons\web\controllers\dataset.py", line 20, in _call_kw
    return call_kw(request.env[model], method, args, kwargs)
  File "D:\odoo-17.0\odoo\api.py", line 468, in call_kw
    result = _call_kw_multi(method, model, args, kwargs)
  File "D:\odoo-17.0\odoo\api.py", line 453, in _call_kw_multi
    result = method(recs, *args, **kwargs)
  File "d:\odoo-17.0\addons\web\models\models.py", line 1030, in onchange
    record._update_cache(changed_values)
  File "D:\odoo-17.0\odoo\models.py", line 6008, in _update_cache
    invf._update(inv_recs, self)
  File "D:\odoo-17.0\odoo\fields.py", line 4173, in _update
    records.modified([self.name])
  File "D:\odoo-17.0\odoo\models.py", line 6723, in modified
    todo = [self._modified([self._fields[fname] for fname in fnames], create)]
  File "D:\odoo-17.0\odoo\models.py", line 6723, in 
    todo = [self._modified([self._fields[fname] for fname in fnames], create)]
KeyError: 'contact_ids'

Where I am going wrong? Is there any other ways to implement the same? Any help is appreciated.
Ảnh đại diện
Huỷ bỏ
Tác giả

For some reason view xml file is not displaying. So I'll post view xml content here.
<record id="view_res_company_form_inherit" model="ir.ui.view">
<field name="name">res.company.form.inherit</field>
<field name="model">res.company</field>
<field name="inherit_id" ref="base.view_company_form"/>
<field name="arch" type="xml">
<xpath expr="//page[@name='branches']" position="attributes">
<attribute name="invisible">1</attribute>
</xpath>

<xpath expr="//field[@name='partner_id']" position="replace">
<field name="comp_industry_type"/>
</xpath>

<xpath expr="//page[@name='branches']" position="after">
<page name="contact_details" string="Contact Details">
<field name="contact_ids">
<tree editable="bottom">
<field name="name"/>
<field name="phone"/>
<field name="email"/>
<field name="function"/>
</tree>
<form string="Contact Details">
<group>
<field name="name"/>
<field name="phone"/>
<field name="email"/>
<field name="function"/>
<field name="street"/>
<field name="city"/>
<field name="zip"/>
<field name="country_id"/>
</group>
</form>
</field>
</page>
</xpath>
</field>
</record>

Câu trả lời hay nhất

Hi,

The parent_id field already exists in res.partner and is used to establish the hierarchical relationship between partners (companies and their contacts). By default, it's a Many2one field pointing to res.partner, not res.company. You shouldn't redefine it to point to res.company as this will break the standard Odoo behavior. And also, like parent_id, is_company is an existing field in res.partner. You don't need to redefine it; it's already there with the default value False.


Hope it helps.

Ảnh đại diện
Huỷ bỏ
Tác giả

Thanks a lot. Appreciate the help.

Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 7 25
1976
1
thg 6 25
2292
2
thg 5 25
1962
1
thg 5 25
1183
1
thg 2 25
38