3

I want to prevent the selected record to show again in the combo box.

enter image description here

As you can see, the 710 - Maleo show again after I selected that record before.

Field declaration for One2many field

class RMReservationOrderLine(models.Model):
    _name = "rm.reservation.order.line"
    _description = "Reservation Order Line"

    room_line_ids = fields.One2many('rm.reservation.room.line', 'order_id', string='Rooms')

Model class for One2many field

class RMReservationRoomLine(models.Model):
    _name = "rm.reservation.room.line"
    _description = "Reservation Room Line"

    order_id = fields.Many2one('rm.reservation.order.line', string='Order', required=True, ondelete='cascade')
    room_id = fields.Many2one('rm.room', string='Room', required=True)

UPDATE

Since my model class for the One2many field just have a single field, room_id, I just change the One2many field to Many2many. Because by default Many2many field prevent duplicate record.

enter image description here

But I still want to know how to prevent duplicate records if I use the One2many field, In case I have more than 1 field in the model class for One2many.

NM Naufaldo
  • 1,032
  • 1
  • 12
  • 30

1 Answers1

1

I think this is the same case as you want.

I already modified the Sales Order, so when the product in the sales order line it's already selected then the product will not be shown again in the selected product.

I used odoo-14 and inherited the sales.order.line and modified the function product_id_change() to become:

@api.onchange('product_id')
def product_id_change(self):
    values = super(SaleOrderLine, self).product_id_change()
    filter_product_ids = [data.product_id.id for data in self.order_id.order_line]
    if values is None:
        values = {}
    values['domain'] = {'product_id' : [('id', 'not in', filter_product_ids)]}
    return values
Dipen Shah
  • 2,396
  • 2
  • 9
  • 21
Rinaldi
  • 260
  • 3
  • 9