تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
8 الردود
18672 أدوات العرض

 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?

الصورة الرمزية
إهمال
أفضل إجابة

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.

الصورة الرمزية
إهمال
الكاتب

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.

أفضل إجابة

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


الصورة الرمزية
إهمال
الكاتب

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

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

الكاتب

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

الكاتب أفضل إجابة

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' ))
الصورة الرمزية
إهمال
أفضل إجابة

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.

الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
3
أبريل 15
10220
0
يناير 20
3667
0
يونيو 19
3359
0
ديسمبر 18
5342
4
مارس 18
6116