3

Need to pass list of ids to sql query in where clause. product_id is many2many field. My code is

query = """
                    SELECT pt.name as product,sl.price_unit as price,sum(sl.qty_invoiced) as quantity from sale_order_line sl
                    JOIN sale_order so ON so.id = sl.order_id
                    JOIN product_product pp ON pp.id = sl.product_id
                    JOIN product_template pt ON pt.id = pp.product_tmpl_id
                    WHERE so.date_order >='"""+str(obj.start_date)+"""' 
                    and so.date_order <= '"""+str(obj.end_date)+"""' 
                    and so.partner_id ="""+str(obj.customer_id.id)+""" 
                    and sl.invoice_status = 'invoiced'
        """ 
    if obj.product_id:
        query +=""" and sl.product_id in """+str(obj.product_id.ids)
    query += """GROUP BY product,price"""

syntax error sl.product_id in [13017, 11253, 1395] near '['

2
  • just curious, what circumstance will use this method? Commented Jul 19, 2019 at 12:51
  • 1
    @TerrencePoe for my report. product_id is many2many field. so if there are two or more products need report of that products only Commented Jul 20, 2019 at 7:30

3 Answers 3

2

Got solution, convert list of ids to tuple

if obj.product_id:
        query +=""" and sl.product_id in %s"""
    query += """GROUP BY product,price"""
    self.env.cr.execute(query, [tuple(obj.product_id.ids)])
Sign up to request clarification or add additional context in comments.

Comments

2

It's not recommanded to render the params directly in the query like that it's a bad habit you should not do it in any programming language (rick of SQL injection).

just use a tuple of params that you pass to execute call.

        query = """
                            SELECT pt.name as product,sl.price_unit as price,sum(sl.qty_invoiced) as quantity 
                            FROM sale_order_line sl
                                JOIN sale_order so ON so.id = sl.order_id
                                JOIN product_product pp ON pp.id = sl.product_id
                                JOIN product_template pt ON pt.id = pp.product_tmpl_id
                            WHERE so.date_order >= %s 
                                and so.date_order <= %s 
                                and so.partner_id = %s 
                                and sl.invoice_status = 'invoiced'
                """ 


        # remember to keep the params in the same order when you use a tuple for params
        # you can use dictionary, you can read about it, instead of %s you write %(some_key)s
        query_params = (obj.start_date,
                        obj.end_date,
                        obj.customer_id.id)
        if obj.product_id:
                query += """ and sl.product_id in  %s """
                query_params += (tuple(obj.product_id.ids),)

        query += """ GROUP BY product,price """

        self.env.cr.execute(query, query_params)

3 Comments

While using getting an error TypeError: not all arguments converted during string formatting
Thanks for the response, I've updated my answer in order to solve the above mentioned issue
yes sorry my mistake forget to put the tuple of ids in a tuple it self query_params += (tuple(obj.product_id.ids),)
1

In SQL a IN clause should use () not [] so try format your IN sequence of value in this way

sl.product_id in (3017, 11253, 1395) 

2 Comments

Thanks for your response. i already know that sql take () not []. Is there any simple way to convert [13017, 11253, 1395] to (3017, 11253, 1395)
Use tuple() to convert the list of ids.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.