Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Odoo中的SQL查询中使用ID列表_Sql_Python 3.x_Postgresql_Odoo - Fatal编程技术网

在Odoo中的SQL查询中使用ID列表

在Odoo中的SQL查询中使用ID列表,sql,python-3.x,postgresql,odoo,Sql,Python 3.x,Postgresql,Odoo,需要在where子句中将ID列表传递给sql查询。产品标识有很多字段。 我的代码是 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

需要在where子句中将ID列表传递给sql查询。产品标识有很多字段。 我的代码是

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"""
query=”“”
选择pt.name作为产品,sl.price\U unit作为价格,sum(sl.qty\U发票)作为销售订单行sl中的数量
加入销售订单so ON so.id=sl.order\u id
在pp.id=sl.product\U id上加入product\U product pp
在pt.id=pp.product\u tmpl\u id上加入产品模板pt
其中so.date\u order>='“”“+str(obj.start\u date)+”

SQL中的so.date_orderIn子句应该使用()而不是[],所以请尝试以这种方式按值顺序设置格式

sl.product_id in (3017, 11253, 1395) 
找到了解决办法, 将ID列表转换为元组

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

不建议直接在查询中呈现参数 就像这是一个坏习惯一样,你不应该在任何编程语言中使用它(SQL注入的里克)

只需使用传递给执行调用的参数元组

        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)
query=”“”
选择pt.name作为产品,sl.price\U unit作为价格,sum(sl.qty\U发票)作为数量
来自销售订单行sl
加入销售订单so ON so.id=sl.order\u id
在pp.id=sl.product\U id上加入product\U product pp
在pt.id=pp.product\u tmpl\u id上加入产品模板pt
其中so.date\u order>=%s

谢谢你的回复。我已经知道sqltake()不是[]。有没有简单的方法可以将[13017112531395]转换为(3017112531395)使用
tuple()
转换
id
的列表。只是好奇,在什么情况下会使用这种方法?@TerrencePoe为我的报告。产品标识有很多字段。因此,如果有两个或更多的产品只需要在使用Get-error TypeError时报告该产品:并非所有参数都在字符串格式期间转换感谢您的响应,我已经更新了我的答案以解决上述问题。很抱歉,我的错误忘记将ID元组放入元组it self
query\u params+=(元组(对象产品标识),)