[Python] Relationships in Odoo?

Discussão em 'Python' iniciado por Stack, Novembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to make a Odoo module to test CRUD functions, I'm trying to use a relationship between 2 PostGreSQL tables that I've already created, first, I used a view to make Odoo contact with the database, but it can only read data, not delete, create or update, so I decided to change to use the real tables instead, but I cannot make it to work, here is what I have:

    from odoo import models, fields

    class PcClientes(models.Model):
    _name = 'clientes.pc_clientes'
    _description = 'Clientes PC'
    _table = 'pc_clientes' # Nombre de la tabla en la base de datos
    _auto = False # No queremos que Odoo cree la tabla automáticamente, ya existe

    # Campos de la tabla pc_clientes
    xempgen_id = fields.Char(string='EmpGen ID', required=True)
    xcliente_id = fields.Char(string='Cliente ID', required=True)
    xcod_postal = fields.Char(string='Código Postal')
    xdomicilio = fields.Char(string='Domicilio')
    xnif = fields.Char(string='NIF')
    xnif_ue = fields.Char(string='NIF UE')
    xnomabrev = fields.Char(string='Nombre Abreviado')
    xnombre = fields.Char(string='Nombre', required=True)
    xpais_id = fields.Char(string='ID País')
    xper_fiscal = fields.Char(string='Período Fiscal')
    xpoblacion = fields.Char(string='Población')
    xprovincia_id = fields.Char(string='ID Provincia')
    xlatitud = fields.Float(string='Latitud')
    xlongitud = fields.Float(string='Longitud')
    trial723 = fields.Char(string='Trial723')

    # Relación con la tabla pl_clientes
    pl_cliente_id = fields.Many2one(
    'clientes.pl_clientes',
    string="PL Cliente",
    ondelete='cascade',
    required=True
    )

    class PlClientes(models.Model):
    _name = 'clientes.pl_clientes'
    _description = 'Clientes PL'
    _table = 'pl_clientes' # Nombre de la tabla en la base de datos
    _auto = False # No queremos que Odoo cree la tabla automáticamente, ya existe

    # Campos de la tabla pl_clientes
    xcliente_id = fields.Char(string='Cliente ID', required=True)
    xcod_docto = fields.Char(string='Código Documento')
    xcopias_alb = fields.Integer(string='Copias Albarán')
    xcopias_fact = fields.Integer(string='Copias Factura')
    xmodalidad_id = fields.Char(string='ID Modalidad')
    xpor_comision = fields.Float(string='Porcentaje Comisión')
    xreferencia = fields.Char(string='Referencia')
    xrepresentante_id = fields.Char(string='ID Representante')
    xsecpref_id = fields.Char(string='ID SecPref')
    xtipo_fact = fields.Integer(string='Tipo de Factura', required=True)
    xempgen_id = fields.Char(string='EmpGen ID', required=True)
    xempresa_id = fields.Char(string='Empresa ID', required=True)
    xfax = fields.Char(string='Fax')
    xtelefono = fields.Char(string='Teléfono')
    xemail = fields.Char(string='Email')
    xfecha_alta = fields.Datetime(string='Fecha Alta')
    xfecha_baja = fields.Datetime(string='Fecha Baja')
    xinternet = fields.Char(string='Internet')
    xlangage = fields.Integer(string='Lenguaje')
    xlangage_remplace = fields.Integer(string='Lenguaje Reemplazado')
    xcopias_ped = fields.Integer(string='Copias Pedido')
    xcredito = fields.Float(string='Crédito')
    xctacon = fields.Char(string='Cta Con')
    xctamayor = fields.Char(string='Cta Mayor')
    xcyc_activo = fields.Integer(string='Cyc Activo')
    xcyc_poliza = fields.Char(string='Cyc Póliza')
    xdivisa_id = fields.Char(string='ID Divisa')
    xdto_com = fields.Float(string='Dto. Comercial')
    xcoment_albaran = fields.Char(string='Comentario Albarán')
    xcoment_factura = fields.Char(string='Comentario Factura')
    xcoment_pedido = fields.Char(string='Comentario Pedido')
    xcontacto = fields.Char(string='Contacto')
    xagrupa_alb = fields.Integer(string='Agrupa Albarán')
    xalb_valorado = fields.Integer(string='Albarán Valorador')
    xalta_modif = fields.Integer(string='Alta Modificada')
    xcalificacion_id = fields.Char(string='ID Calificación')
    xcargo = fields.Char(string='Cargo')
    xrecibe_facturae = fields.Integer(string='Recibe Facturae')
    xxjuego_id = fields.Char(string='ID Juego')
    p856_tipo_tarifa = fields.Char(string='Tipo Tarifa')
    p856_dto_metraje = fields.Float(string='Dto. Metraje')
    p856_dto_tarifa = fields.Float(string='Dto. Tarifa')
    p856_agen_com_id = fields.Char(string='Agente Com')
    p856_eti_metr_ext = fields.Char(string='Etiqueta Metr Ext')
    p856_observaciones = fields.Char(string='Observaciones')
    p856_cadena = fields.Char(string='Cadena')
    p856_credito_aseg = fields.Float(string='Crédito Asegurado')
    p856_credito_rioma = fields.Float(string='Crédito Rioma')
    p856_crm_id = fields.Char(string='ID CRM')
    p856_modificado = fields.Integer(string='Modificado')
    p856_modificado_r = fields.Integer(string='Modificado R')
    p856_dto_met_max = fields.Float(string='Dto. Metraje Max')
    p856_dto_pie_max = fields.Float(string='Dto. Pie Max')
    p856_entite = fields.Char(string='Entidad')
    p856_pass_crm = fields.Char(string='Pass CRM')
    p856_cerrado = fields.Integer(string='Cerrado')
    p856_supervisor = fields.Char(string='Supervisor')
    p856_tipo_cliente = fields.Integer(string='Tipo Cliente')
    p856_fact_ftp = fields.Integer(string='Fact. FTP')
    p856_backoffice_id = fields.Char(string='ID Backoffice')
    trial726 = fields.Char(string='Trial 726')

    # Relación con la tabla pc_clientes
    pc_cliente_id = fields.Many2one(
    'clientes.pc_clientes',
    string="PC Cliente",
    ondelete='cascade',
    required=True
    )


    That's my models.py

    <odoo>
    <data>

    <record id="clientes_list_view" model="ir.ui.view">
    <field name="name">clientes.list.view</field>
    <field name="model">clientes.pc_clientes</field>
    <field name="arch" type="xml">
    <list>
    <field name="xempgen_id"/>
    <field name="xcliente_id"/>
    <field name="xnomabrev"/>
    <field name="xnombre"/>
    <field name="pl_cliente_id.xfax"/>
    </list>
    </field>
    </record>

    <record id="clientes_form_view" model="ir.ui.view">
    <field name="name">clientes.form.view</field>
    <field name="model">clientes.pc_clientes</field>
    <field name="arch" type="xml">
    <form>
    <group name="Datos indicativos" string="Datos indicativos" class="central">

    <group>
    <field name="xnombre" class="field"/>
    </group>
    <group>
    <field name="xnomabrev" class="field"/>
    </group>
    <group>
    <field name="xreferencia" class="field"/>
    </group>
    <field name="pl_cliente_id.xfax" class="field"/>
    <group/>



    </group>
    </form>
    </field>
    </record>

    <record id="clientes_action" model="ir.actions.act_window">
    <field name="name">Clientes</field>
    <field name="res_model">clientes.pc_clientes</field>
    <field name="view_mode">list,form</field>
    </record>

    <menuitem id="menu_root" name="Rioma"/>
    <menuitem id="menu_clientes" name="Clientes" parent="menu_root"/>
    <menuitem id="menu_clientes_list" name="Clientes List"
    parent="menu_clientes" action="clientes_action"/>

    </data>
    </odoo>


    That's my view.

    When I run the odoo server, it gives me this error:

    Error while validating view near:

    <field name="xnombre"/>
    <field name="pl_cliente_id.xfax"/>
    </list>
    Field "pl_cliente_id.xfax" does not exist in model "clientes.pc_clientes"

    View error context:
    {'file': 'c:\\odoo\\modules\\clientes\\views\\templates.xml',
    'line': 7,
    'name': 'clientes.list.view',
    'view': ir.ui.view(878,),
    'view.model': 'clientes.pc_clientes',
    'view.parent': ir.ui.view(),
    'xmlid': 'clientes_list_view'}


    I'm new at Odoo, thank you so much for your help guys

    Continue reading...

Compartilhe esta Página