Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
8 Trả lời
18698 Lượt xem

 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?

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Tác giả

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.

Câu trả lời hay nhất

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


Ảnh đại diện
Huỷ bỏ
Tác giả

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

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

Tác giả

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

Tác giả Câu trả lời hay nhất

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' ))
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 4 15
10255
0
thg 1 20
3683
0
thg 6 19
3378
0
thg 12 18
5375
4
thg 3 18
6146