Home > Enterprise >  Odoo - Show another value instead of the object identifier
Odoo - Show another value instead of the object identifier

Time:12-11

Good afternoon. I am trying that, in the following view, among the possible options for "tipoCargo", it shows me the name of the corresponding employee position but instead it shows me values that follow the structure "model.class: id". I am attaching code for both the view and the models involved in case someone has had a similar problem. Best regards and sorry for the inconvenience.

Empleados model

from odoo import models, fields, api

class Empleados(models.Model):
    _name = 'upobebe.empleados'
    _description = 'Clase empleados para el proyecto UPOBebe'

    dni_empleado = fields.Char("DNI del empleado", size=9, required=True, help="DNI del empleado")
    nombre = fields.Char("Nombre del empleado", size=60, required=True, help="Nombre del empleado")
    apellidos = fields.Char("Apellidos del empleado", size=90, required=True, help=" Apellidos del empleado")
    correo = fields.Char("Correo de contacto")
    fechaNacimiento = fields.Date("Fecha de nacimiento")
    fechaAlta = fields.Date("Fecha de alta", required=True)
    
    transacciones = fields.One2many("upobebe.empleados", 'dni_empleado', 'Empleados')

    tipoCargo = fields.Many2one("upobebe.cargo", string="Cargo")

Cargo model

from odoo import models, fields, api
class cargo(models.Model):
    _name = 'upobebe.cargo'
    _description = 'Lista de cargos'

    id_cargo = fields.Char("Cargo del empleado",required=True)
    
    transacciones = fields.One2many("upobebe.empleados", 'tipoCargo', 'Cargo')

    _sql_constraints = [('cargo_id_cargo_unique', 'UNIQUE (id_cargo)', 'El identificador del cargo debe ser unico')]

Empleado view

<odoo>
    <record id="upobebe_empleado_form_view" model="ir.ui.view">
        <field name="name">upobebe.empleados.form</field>
        <field name="model">upobebe.empleados</field>
        <field name="arch" type="xml">
            <form string="Empleados">
                <sheet layout="auto">
                    <group>
                        <field name="dni_empleado" />
                        <field name="nombre" />
                        <field name="apellidos" />
                    </group>
                    <group>
                        <field name="correo" />
                        <field name="fechaNacimiento" />
                        <field name="fechaAlta" />
                        <field name="tipoCargo" widget="many2one" options="{'no_open': True}" />
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <record id="upobebe_empleado_tree_view" model="ir.ui.view">
        <field name="name">upobebe.empleados.tree</field>
        <field name="model">upobebe.empleados</field>
        <field name="arch" type="xml">
            <tree string="tree_string">
                <field name="dni_empleado" />
                <field name="apellidos" />
                <field name="nombre" />
                <field name="correo" />
            </tree>
        </field>
    </record>

    <record id="empleado_list_action" model="ir.actions.act_window">
        <field name="name">Empleado</field>
        <field name="res_model">upobebe.empleados</field>
        <field name="view_mode">tree,form</field>
        <field name="help" type="html">
            <p >Crear empleados</p>
        </field>
    </record>
</odoo>

CodePudding user response:

You can use the _rec_name attribute, it is used for labeling records (name is the default)

Example:

_rec_name = "id_cargo"

You can use a computed field to show customized labels

  • Related