0

inherited " res.partner " and added a page (editable tree) in notebook section, but when clicking on "Add a line" it is showing below error:

Invalid field 'same_vat_partner_id' on model 'vehicle.brand'

My code to inherit res.partner and add One2many fields in it :

from odoo import api, fields, models


class CustomContacts(models.Model):
_inherit = "res.partner"


x_brand_ids = fields.Many2many('res.partner.line', 'x_brand_id', string="Brand Name")
x_model_ids = fields.One2many('res.partner.line', 'x_model_id', string="Model Name")


class CustomContactsPage(models.Model):
_name = "res.partner.line"

x_brand_id = fields.Many2one('vehicle.brand', string="Brand Name")
x_model_id = fields.Many2one('vehicle.model', string="Model Name")

My code of vehicle.brand model :

from odoo import api, fields, models


class BrandCreate(models.Model):
_name = "vehicle.brand"
_description = "Customer"

name = fields.Char(string='Brand Name', required=True)

My code of vehicle.model model :

from odoo import api, fields, models


class ModelName(models.Model):
_name = "vehicle.model"
_description = "Models"


name = fields.Char(string='Model Name', required=True)

My code of view :

<?xml version="1.0" encoding="utf-8"?>
<record id="view_partner_form_inherited" model="ir.ui.view">
    <field name="name">res.partner.inherited</field>
    <field name="model">res.partner</field>
    <field name="inherit_id" ref="base.view_partner_form"/>
    <field name="arch" type="xml">
        <xpath expr="//page[@name='internal_notes']" position="after">
            <page string="Vehicle Details">
                    <field name="x_brand_ids"></field>
                    <field name="x_model_ids"></field>
            </page>
        </xpath>
    </field>
</record>

Getting view inside view as mentioned in below images :

view 1

view 2

2 Answers2

1

When using a One2many field the inverse_name (x_model_id in your example) is equal to the current record and Odoo tries to recompute the partner vat using vehicle.model record in place of a res.partner record and this is why you are getting that error.

To fix it just define the inverse name in vehicle.brand model and use it in x_model_ids field instead of x_model_id.

Example:

class CustomContacts(models.Model):
    _inherit = "res.partner"

    x_model_ids = fields.One2many('res.partner.line', 'model_id', string="Model Name")


class CustomContactsPage(models.Model):
    _name = "res.partner.line"

    model_id = fields.Many2one('res.partner')

Edit:

If you want to edit the One2many field in place, define it inside the form view like following:

<field name="x_model_ids">
    <tree editable="bottom">

    </tree>
</field>

For the Many2many fields, Odoo will open a dialog to select or create vehicle brands.

Kenly
  • 24,317
  • 7
  • 44
  • 60
  • Thanks for answer but do i need to add res.partner as comodel name in model_id. I think there i need to add vehicle.model to fetch its data. ?? – Dhiren Ahuja Apr 14 '22 at 10:35
  • The comodel should be `res.partner` in my example above and if you need to link `res.partner.line` to `vehicle.model` keep the `x_model_id` field. My example shows only how to add a one2many field in the res partner model that let you add partner lines (`res.partner.line`). – Kenly Apr 14 '22 at 10:44
  • This is how Odoo is designed, when you use a one2many field in model `A` with comodel `B`, you should add the inverse relation (a Many2one field) in model `B` that references model A and use it as the `inverse_name`. – Kenly Apr 14 '22 at 10:48
  • Define a tree view for `vehicle.brand` and `vehicle.vehicle` models and Odoo will automatically use them as subviews. – Kenly Apr 14 '22 at 10:57
-1

You needs to understand the Many2many and One2many fields. For many2many you check the required argument is the model name only. and other are optional. (Filling Many2many field (odoo 8))

x_brand_ids = fields.Many2many('res.partner.line', string="Brand Name")

For adding One2many we need many2one with that same model.

x_model_ids = fields.One2many('res.partner.line', 'x_model_id', string="Model Name")

For this you need x_model_id Many2one of res.partner field under res.partner.line

You need to like below:

class CustomContacts(models.Model):
_inherit = "res.partner"


x_brand_ids = fields.Many2many('res.partner.line', string="Brand Name")
x_model_ids = fields.One2many('res.partner.line', 'x_model_id', string="Model Name")


class CustomContactsPage(models.Model):
_name = "res.partner.line"

x_brand_id = fields.Many2one('vehicle.brand', string="Brand Name")
x_model_id = fields.Many2one('res.partner', string="Model Name")
  • Thank you for answering it worked but why i am getting view inside view ?? I have edited the question and added 2 images which is the view i am getting. – Dhiren Ahuja Apr 14 '22 at 10:48