Python Openerp v7 API函数字段';one2many';类型

Python Openerp v7 API函数字段';one2many';类型,python,openerp,openerp-7,Python,Openerp,Openerp 7,单击treeview记录时,我想打开一个formview。当formview打开时,我有两个one2many字段需要动态填充(在我的例子中,我有一个产品列表,单击一个产品时,我想搜索该产品的库存移动次数) 我想要的是类似于字段的功能。function字段。这应该是可行的,但它不能: 'last\u sales':fields.function(\u get\u last\u sales,type='one2many',relation='stock.move',readonly=True,sto

单击treeview记录时,我想打开一个formview。当formview打开时,我有两个one2many字段需要动态填充(在我的例子中,我有一个产品列表,单击一个产品时,我想搜索该产品的库存移动次数)

我想要的是类似于
字段的功能。function
字段。这应该是可行的,但它不能:

'last\u sales':fields.function(\u get\u last\u sales,type='one2many',relation='stock.move',readonly=True,store=store\u触发器)

服务器回溯(最近一次呼叫最后一次):
文件“/home/george/odoo/mattia_v7/openerp/addons/web/session.py”,第89行,在send中
返回openerp.netsvc.dispatch\u rpc(服务名称、方法、参数)
文件“/home/george/odoo/mattia_v7/openerp/netsvc.py”,第296行,在dispatch_rpc中
结果=ExportService.getService(服务名称).dispatch(方法,参数)
文件“/home/george/odoo/mattia_v7/openerp/service/web_services.py”,第626行,已发送
res=fn(db,uid,*params)
文件“/home/george/odoo/mattia_v7/openerp/osv/osv.py”,第190行,在execute_kw中
返回self.execute(db、uid、obj、method、*args、**kw或{})
文件“/home/george/odoo/mattia_v7/openerp/osv/osv.py”,第132行,在包装器中
返回f(self、dbname、*args、**kwargs)
文件“/home/george/odoo/mattia_v7/openerp/osv/osv.py”,第199行,执行
res=self.execute\u cr(cr,uid,obj,method,*args,**kw)
文件“/home/george/odoo/mattia_v7/openerp/osv/osv.py”,第187行,在execute_cr中
返回getattr(对象、方法)(cr、uid、*args、**kw)
文件“/home/george/odoo/mattia_v7/openerp/osv/orm.py”,第3718行,已读
结果=自读平面(cr、用户、选择、字段、上下文、加载)
文件“/home/george/odoo/mattia_v7/openerp/osv/orm.py”,第3841行,平铺
res2=self.\u列[f].get(cr,self,ids,f,user,context=context,values=res)
get中的文件“/home/george/odoo/mattia_v7/openerp/osv/fields.py”,第1152行
结果=self.\u fnct(对象、cr、uid、ID、名称、self.\u参数、上下文)
文件“/home/george/odoo/mattia_v7/openerp/addons/product/product.py”,第461行,产品价格
res[product.id]=product.list\u价格
文件“/home/george/odoo/mattia_v7/openerp/osv/orm.py”,第503行,在__
返回自我[姓名]
文件“/home/george/odoo/mattia_v7/openerp/osv/orm.py”,第469行,在__
elif field_column._输入('one2many','many2many')和len(结果行[字段名称]):
类型错误:“bool”类型的对象没有len()

我能看见你的眼睛

这似乎没有得到支持。
答案正确吗?这方面有什么解决办法吗?

请尝试以下代码:

def get_last_sales(self, cr, uid, ids, context=None):

    customer_loc_id = self.pool.get('stock.location').search(cr, uid, [('usage', '=', 'customer')])
    result = {}

    if customer_loc_id:

        for record in self.browse(cr, uid, ids):

            result[record.id] = self.pool.get('stock.move').search(cr, uid, [('product_id', '=', record.id), ('location_dest_id', '=', customer_loc_id[0])])

    return result

def get_last_productions(self, cr, uid, ids, context=None, args=None, kwargs=None):

    production_loc_id = self.pool.get('stock.location').search(cr, uid, [('usage', '=', 'production')])
    result = {}

    if production_loc_id:

        for record in self.browse(cr, uid, ids):

            result[record.id] =  self.pool.get('stock.move').search(cr, uid, [('product_id', '=', record.id), ('location_dest_id', '=', production_loc_id[0])])

    return result

'last_sales':fields.function(_get_last_sales, type='one2many', relation='stock.move', readonly=True, string='Last Sales')

请尝试使用以下代码:

def get_last_sales(self, cr, uid, ids, context=None):

    customer_loc_id = self.pool.get('stock.location').search(cr, uid, [('usage', '=', 'customer')])
    result = {}

    if customer_loc_id:

        for record in self.browse(cr, uid, ids):

            result[record.id] = self.pool.get('stock.move').search(cr, uid, [('product_id', '=', record.id), ('location_dest_id', '=', customer_loc_id[0])])

    return result

def get_last_productions(self, cr, uid, ids, context=None, args=None, kwargs=None):

    production_loc_id = self.pool.get('stock.location').search(cr, uid, [('usage', '=', 'production')])
    result = {}

    if production_loc_id:

        for record in self.browse(cr, uid, ids):

            result[record.id] =  self.pool.get('stock.move').search(cr, uid, [('product_id', '=', record.id), ('location_dest_id', '=', production_loc_id[0])])

    return result

'last_sales':fields.function(_get_last_sales, type='one2many', relation='stock.move', readonly=True, string='Last Sales')

是的,存储许多函数或计算字段毫无意义。为什么?这两种类型都需要一些特殊的设置,而不能在字段定义上进行设置。在one2many上,您需要在另一个型号上使用相关的外键。在许多情况下,必须为两个模型键定义表

我使用odoo的经验表明:使用许多未存储的函数字段(函数需要在其常用字典中返回ID列表)


另外,正如@Odedra在回答中提到的,您的函数是不正确的

是的,存储许多函数或计算字段毫无意义。为什么?这两种类型都需要一些特殊的设置,而不能在字段定义上进行设置。在one2many上,您需要在另一个型号上使用相关的外键。在许多情况下,必须为两个模型键定义表

我使用odoo的经验表明:使用许多未存储的函数字段(函数需要在其常用字典中返回ID列表)


另外,正如@Odedra在回答中提到的,您的函数是不正确的

请使用“获取最后一次”销售方法更新您的问题。@Odedra更新了我的问题请使用“获取最后一次”销售方法更新您的问题。@Odedra更新了我的问题请更具体一点,这段代码是做什么的?我为什么要写这个?这个代码可以解决你的问题。在您的代码中,存在许多问题。例如,您没有将值传递给特定的记录id。当我们使用=运算符时,则需要设置适当的值。在您的情况下,您已经通过了列表。(客户地址)和(生产地址)感谢您在Odedra的时间和回复。但我的主要问题不在于这些方法。我的主要问题是当我的记录的表单视图打开时如何调用方法。请忽略这些方法,我已经将它们删除了,我只是插入它们以查看它们是否会被调用。请更具体地说,这段代码是做什么的?我为什么要写这个?这个代码可以解决你的问题。在您的代码中,存在许多问题。例如,您没有将值传递给特定的记录id。当我们使用=运算符时,则需要设置适当的值。在您的情况下,您已经通过了列表。(客户地址)和(生产地址)感谢您在Odedra的时间和回复。但我的主要问题不在于这些方法。我的主要问题是当我的记录的表单视图打开时如何调用方法。请忽略这些方法,我已经删除了它们,我只是插入了它们,看看它们是否会被调用。