Skip to Content
Menu
This question has been flagged
1 Reply
4359 Views

hi all iam a newbie to odoo , what iam trying to achieve is i want to prevent from adding duplicate products in order line , if the same exits already we should not able to add the duplicate product 


    @ api.multi

    @ api.constrains ('order_line')

    def _check_exist_product_in_line (self):

      for rec in self:

          product_id = self.env ['product.product']. search ([('default_code', '=', 'MIFFLIN')])

          for line in self.order_line:

             if line.product_id.id in product_id:

                raise ValidationError (_ ('Product already added.'))

             product_id.append (line.product_id.id)



i tried like this but it's not working its throwing operation not permitted. what am i missing here , someone help me here

Avatar
Discard
Author

TypeError: Mixing apples and oranges: product.product(28,) in sale.order.line(572,)

Best Answer

Hello Mascherano,

Comparing what you want to achieve with what your code does seem like two different things to me.

I feel your code will solve your situation, you can just fix it like the following:

  1. Please do not use @api.multi & @api.constrains on the same function

  2. the result of self.env ['product.product']. search ([('default_code', '=', 'MIFFLIN')]) is product.product object set and not an integer and you are trying to compare this set of objects with an integer on if line.product_id.id in product_id (line.product_id.id is an integer value) ==> This throws the error: TypeError: Mixing apples and oranges

  3. Bellow is the fix of your code:

@api.constrains ('order_line')
def _check_exist_product_in_line(self):
product_ids = self.env['product.product'].search([('default_code', '=', 'MIFFLIN')])
for rec in self:
for line in rec.order_line:
if line.product_id in product_ids:
raise ValidationError(_('Product already added.'))
product_id.append(line.product_id.id)
    return True

Let me explain a little bit what's your code is for:

  1. Get all the products that has 'MIFFLIN' as a default code.

  2. You loop on the Sale order lines

  3. If the line's product is one of the products searched on step 1 then raise an error.

  4. So your code will always raise an error if 'MIFFLIN' product is present on sale order lines even that is not duplicated (exist only once).

Honestly, i don't believe your code will help you to achieve your explained goal, If you feel the same, please to use the bellow code:

@api.constrains ('order_line')
def _check_exist_product_in_line(self):
for order in self:
products_in_lines = order.mapped('order_line.product_id')
for product in products_in_lines:
lines_count = len(order.order_line.filtered(lambda line: line.product_id == product))
if lines_count > 1:
raise ValidationError(_('Product already added.'))
return True

Hope this will help,

Good luck.

Avatar
Discard
Author

when i try to add the product , it showing as error product_id is not defined

product_ids = []
for line in self.order_line:
product_ids.append (line.product_id.id)
occurrences = product_ids.count (self.product_id.id)
if occurrences> 2:
raise ValidationError ('You cannot select already added product.' )

Onchange product_id

Related Posts Replies Views Activity
1
Jun 23
3426
3
Mar 23
14812
0
Oct 21
1283
1
Nov 24
1616
3
Nov 23
15312