Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to check if exist the same Product in the line ( Field One2many)??

By
Achref Mhadhbi
on 11/5/15, 10:27 AM 698 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?

2

Akhil P Sivan

--Akhil P Sivan--
3423
| 5 3 6
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 11/5/15, 12:44 PM

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.

Achref Mhadhbi
on 11/5/15, 12:52 PM

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

Akhil P Sivan
on 11/5/15, 12:55 PM

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

Achref Mhadhbi
on 11/5/15, 1:01 PM
1
Achref Mhadhbi
On 11/5/15, 6:34 PM

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

Sudhir Arya (SA)

--Sudhir Arya (SA)--
10150
| 6 8 8
Ahmedabad, India
--Sudhir Arya (SA)--

Working as an OpenERP/Odoo developer and a Team Leader 

Top 5 Odoo contributor On Stackoverflow

LinkedIn

Blog

Stackoverflow

Sudhir Arya (SA)
On 11/5/15, 12:39 PM

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.

Achref Mhadhbi
on 11/5/15, 12:48 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 11/5/15, 10:27 AM
Seen: 698 times
Last updated: 11/9/15, 9:19 AM