Home > Net >  Many2one field display x.y 1 , x.y 2 ... etc , instead of values in x.y table in odoo 15
Many2one field display x.y 1 , x.y 2 ... etc , instead of values in x.y table in odoo 15

Time:11-01

im trying to display values of x.y in Many2one field , here is my code

this is the module of x.y

from email.policy import default
from odoo import fields,models,api,_
class XY(models.Model):
    _name = 'x.y'
    pat_fullname = fields.Char(string='Full Name')
    pat_age = fields.Integer(string='Age')

XY class is working fine, the problem is in ZX class

from email.policy import default
from odoo import fields,models,api,_
class ZX(models.Model):
    _name = 'z.x'
    _description = 'Dentist Appointment'
    parent_id = fields.Many2one('x.y',string='Patient',required=True)
    note = fields.Text(string='Note')

and this is the xml of Z.X

<record id="view_form_z_x" model="ir.ui.view">
        <field name="name">z.x.view.form</field>
        <field name="model">z.x</field>
        <field name="arch" type="xml">
            <form>
                <sheet>
                    <group>
                        <group>
                            <field name="parent_id"/>
                        </group>
                        <group>
                            <field name="note"/>
                        </group>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

Normally with this code i should see in Patient (Many2one) field (value1,value2,...ect) but i get this in the result (x.y 1 , x.y 2 , ...etc)

can you spot what i did wrong ??

CodePudding user response:

Odoo uses the combination of the model's fields and id as display name at many2one fields, to change it we use _rec_name attribute in x.y model, which contains one of the field's of the model. notice value of _rec_name should be unique for every record , so you can use combination of columns with an arbitrary compute(store = false) field and set it as a rec_name please look at this answer https://stackoverflow.com/a/71814814/12138245

CodePudding user response:

Actually i found out what's wrong with my source code, Many2one field always getting the value of field named name so all i had to do is change pat_fullname to name in XY class and is display the excpected values in Many2one field

  • Related