This question has been flagged
8 Replies
16794 Views

 I have a function create stock picking named : action_picking_create() 

My code :

if len(self.line_ids) == 0:
raise exceptions.Warning(_('No line'))
for line in self.line_ids :
if line.quantity > 0
self.action_picking_create()
else :
raise exceptions.Warning(_('Quantity must be > 0'))

I want to test if I have the same product I want to raise an exception.

I try with

for todo_line in self.line_id
for line in self.line_ids :
if todo_line.product_id.id != line.product_id.id
if line.quantity > 0
self.action_picking_create()
else :
raise exceptions.Warning(
_('Quantity must be > 0'))
else :
raise exceptions.Warning(
_('Product Exist'))

How to check it if exist the same product in the line_ids?

Avatar
Discard
Best Answer

Hi Achref,

You can use search method to search the product in the line.

Ex:

my_line_obj = self.pool.get('my.line') # object of your line_ids field
for line in self.line_id:
my_line_ids = my_line_obj.search(cr, uid, [('parent_id', '=', line.parent_id), ('product_id', '=', line.product_id.id)], context=context)
# do something with my_line_ids

Here, parent_id is a many2one field in the line of parent object (ex: order_id of sale.order relation in sale.order.line)

Hope this will help you.

Avatar
Discard
Author

HI Sudhir Arya Thank your for your reply. I want a code with python to test the product_id if exist in my lines_ids. If the script check duplicate or same product will show me an exception before I call my function.

Best Answer

Hi,

You may try like this:

err_products = []
for line in self.line_ids :
    if line.product_id.id not in err_products:
        if line.quantity > 0
            self.action_picking_create()
        else :
    raise exceptions.Warning(_('Quantity must be > 0'))
err_products.append(line.product_id.id)
else:
raise exceptions.Warning(_('Product Exist'))


Avatar
Discard
Author

Akhil thank you !!! that's I want it.

Hi Achref, if my answer solves your problem, please do mark & upvote

Author

product_id = False product_ids =[] for line in self.line_ids : product_id=line.product_id.id if not ( product_id in product_ids) : product_ids.append(product_id) if line.quantity>0 : self.action_picking_create() else : raise exceptions.Warning( _('Quantity must be > 0')) else : raise exceptions.Warning( _('Product Exit' ))

Author Best Answer

Many Thanks to Akhil.

product_id = False product_ids =[] 
for line in self.line_ids :
product_id=line.product_id.id
if not ( product_id in product_ids) :
product_ids.append(product_id)
    if line.quantity>0 :
       self.action_picking_create()
    else :
        raise exceptions.Warning( _('Quantity must be > 0'))
else : raise exceptions.Warning( _('Product Exit' ))
Avatar
Discard
Best Answer

Dear,

To check exist the same Product in the line level.

from odoo import models, fields, api
from odoo.exceptions import ValidationError


class PurchaseOrder(models.Model):
_inherit = "purchase.order"

@api.multi
@api.constrains('order_line')
def _check_exist_product_in_line(self):
for purchase in self:
exist_product_list = []
for line in purchase.order_line:
if line.product_id.id in exist_product_list:
raise ValidationError(_('Product should be one per line.'))
exist_product_list.append(line.product_id.id)

Hope this code help you

Best Thanks,

Ankit H Gandhi.

Avatar
Discard