SerenityTn
SerenityTn

Reputation: 171

how to use virtual records of one2many field in a many2one field of the same model

In the Sale Order form I added a one2many field named booking_ids which appears within an editable tree view

Within the tree of the order lines, I added a many2one column named booking_id so each orderline have a booking_id.

While in edit mode or create mode of the sale.order how is it possible to show the virtual records added in the booking_ids field within the many2one field which is only showing the records within the database without the virtual ones that are being created.

class SaleOrder(models.Model):
    _inherit = 'sale.order'
    booking_ids = fields.One2many('sale.booking', 'order_id', string='Bookings')

class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'        
    booking_id = fields.Many2one('sale.booking', string="Booking")

class Booking(models.Model):
    _name = 'sale.booking'
    event_id = fields.Many2one('sale.event', String='Event', required=True,
    states={'draft': [('readonly', False)], 'sent': [('readonly', False)]})
    name = fields.Char(compute='_get_name')
    event_date = fields.Date(string='Event Date')
    order_id = fields.Many2one('sale.order', string='Order Reference', ondelete='cascade', index=True, copy=False, readonly=True)
    order_line_ids = fields.One2many('sale.order.line', 'booking_id', string='Order Lines')

XML

         <xpath expr="//field[@name='payment_term_id']/.." position="after">
            <group>                    
                <field name="booking_ids">
                    <tree editable="bottom">
                        <field name="event_id"/>
                        <field name="event_date" string="Date"/>
                        <field name="order_line_ids" widget="many2many_tags" readonly="1"/>
                    </tree>
                </field>
            </group>
        </xpath>

        <xpath expr="//tree//field[@name='product_id']" position="after">      
            <field name="order_id" invisible="1"/>
            <field name="booking_id" domain="['|', ('order_id', '=', None), ('order_id', '=', order_id)]"/>                
        </xpath>    
        <xpath expr="//field[@name='order_line']//field[@name='product_id']/.." position="after">
        <field name="order_id" invisible="1"/>
            <field name="booking_id" domain="['|', ('order_id', '=', None), ('order_id', '=', order_id)]"/>                
        </xpath>

thank you

Upvotes: 0

Views: 432

Answers (1)

Pablo Escobar
Pablo Escobar

Reputation: 685

Use a preload in Python code. This will solve your problem.

Upvotes: 0

Related Questions