Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
8 Răspunsuri
18811 Vizualizări

 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?

Imagine profil
Abandonează
Cel mai bun răspuns

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.

Imagine profil
Abandonează
Autor

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.

Cel mai bun răspuns

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'))


Imagine profil
Abandonează
Autor

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

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

Autor

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' ))

Autor Cel mai bun răspuns

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' ))
Imagine profil
Abandonează
Cel mai bun răspuns

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.

Imagine profil
Abandonează
Related Posts Răspunsuri Vizualizări Activitate
3
apr. 15
10313
0
ian. 20
3795
0
iun. 19
3500
0
dec. 18
5517
4
mar. 18
6231