'字典'对象没有属性'执行'

时间:2015-11-25 13:17:51

标签: python openerp

我有python代码(odoo)

    @api.one
def mps_value(self,cr):
    fc = self.forecast_id
    cr.execute("""SELECT SUM(sol.product_uom_qty) 
                FROM sale_order_line AS sol , sale_order AS s , forecastmps_forecast AS fc , forecastmps_period AS pd 
                WHERE fc.id = %s AND sol.product_id = fc.product_id AND fc.period_id = pd.id AND pd.start_date <= sol.create_date 
                AND sol.create_date <= pd.end_date AND s.state = \'done\' AND sol.order_id = s.id""", (fc,))
    ret = cr.fetchone()[0]
    return ret

我运行它,这是错误

AttributeError: 'dict' object has no attribute 'execute'

我该如何解决?谢谢。

3 个答案:

答案 0 :(得分:2)

你应该尝试以下,

从方法定义中删除cr(游标),并在调用此函数时不传递任何参数。

@api.one
def mps_value(self):
    fc = self.forecast_id.id
    self.env.cr.execute("""SELECT SUM(sol.product_uom_qty) 
                FROM sale_order_line AS sol , sale_order AS s , forecastmps_forecast AS fc , forecastmps_period AS pd 
                WHERE fc.id = %s AND sol.product_id = fc.product_id AND fc.period_id = pd.id AND pd.start_date <= sol.create_date 
                AND sol.create_date <= pd.end_date AND s.state = \'done\' AND sol.order_id = s.id""", (fc,))
    ret = self.env.cr.fetchone()[0]
    return ret

答案 1 :(得分:1)

您正在混合使用v7和v8。 删除cr参数并使用ORM方法来计算该总和。 此外,您返回的值是“丢失”,即不在任何地方使用...

答案 2 :(得分:1)

要执行查询,请尝试以下操作:

self.env.cr.execute(<your query>)

OR

self._cr.execute(<your query>)

新api中的函数参数中不需要cr