Search code examples
odoo

Odoo - Populate field with data


I added a product to a ticket:

class TicketExt(models.Model):
    _inherit = 'helpdesk.support'
    product = fields.Many2many('product.template',string='Product')

Now I want to display the ticket that is related to the product:

class ProductExt(models.Model):
    _inherit = 'product.template'
    p_tickets = fields.Many2many('helpdesk.support','product', string='Tickets' )

<record id="product_template_common_form_ticket_ext" model="ir.ui.view">
    <field name="name">product.template.common.form.ticket.ext</field>
    <field name="model">product.template</field>
    <field name="inherit_id" ref="product.product_template_form_view"/>
    <field name="arch" type="xml">
        <xpath expr="//page[@name='notes']" position="after">
            <page string='Tickets' >
                <field name="p_tickets"  />
            </page>
        </xpath>
    </field>
</record>

I can add new tickets to a product, but the tickets that have been added previously, do not show up.

Thank you for any suggestions


Solution

  • Try to add common relation field and reference fields in your definition.

    product = fields.Many2many('product.template', 'product_ticket_rel', 'ticket_id', 'product_id')
    

    and

    p_ticket = fields.Many2many('helpdesk.ticket','product_ticket_rel','product_id','ticket_id')
    

    See reference on https://www.odoo.com/documentation/14.0/reference/orm.html

    And sometimes it is hard to select which one you should use product.product or product.template