点击树视图记录后,我想打开一个表单视图。打开那个formview时,我有两个one2many字段需要动态填充(在我的情况下,我有一个产品列表,点击一个产品我想搜索已经为这个产品移动了多少股票)
def get_last_sales(self, cr, uid, ids, context=None):
customer_loc_id = self.pool.get('stock.location').search(cr, uid, [('usage', '=', 'customer')])
result = self.pool.get('stock.move').search(cr, uid, [('product_id', '=', ids), ('location_dest_id', '=', customer_loc_id)])
print result
#raise Exception
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 = self.pool.get('stock.move').search(cr, uid, [('product_id', '=', ids), ('location_dest_id', '=', production_loc_id)])
return result
我想要的是与fields.function
字段类似的功能。这应该有效但不是:
'last_sales':fields.function(_get_last_sales, type='one2many', relation='stock.move', readonly=True, store=store_triggers)
Server Traceback (most recent call last):
File "/home/george/odoo/mattia_v7/openerp/addons/web/session.py", line 89, in send
return openerp.netsvc.dispatch_rpc(service_name, method, args)
File "/home/george/odoo/mattia_v7/openerp/netsvc.py", line 296, in dispatch_rpc
result = ExportService.getService(service_name).dispatch(method, params)
File "/home/george/odoo/mattia_v7/openerp/service/web_services.py", line 626, in dispatch
res = fn(db, uid, *params)
File "/home/george/odoo/mattia_v7/openerp/osv/osv.py", line 190, in execute_kw
return self.execute(db, uid, obj, method, *args, **kw or {})
File "/home/george/odoo/mattia_v7/openerp/osv/osv.py", line 132, in wrapper
return f(self, dbname, *args, **kwargs)
File "/home/george/odoo/mattia_v7/openerp/osv/osv.py", line 199, in execute
res = self.execute_cr(cr, uid, obj, method, *args, **kw)
File "/home/george/odoo/mattia_v7/openerp/osv/osv.py", line 187, in execute_cr
return getattr(object, method)(cr, uid, *args, **kw)
File "/home/george/odoo/mattia_v7/openerp/osv/orm.py", line 3718, in read
result = self._read_flat(cr, user, select, fields, context, load)
File "/home/george/odoo/mattia_v7/openerp/osv/orm.py", line 3841, in _read_flat
res2 = self._columns[f].get(cr, self, ids, f, user, context=context, values=res)
File "/home/george/odoo/mattia_v7/openerp/osv/fields.py", line 1152, in get
result = self._fnct(obj, cr, uid, ids, name, self._arg, context)
File "/home/george/odoo/mattia_v7/openerp/addons/product/product.py", line 461, in _product_lst_price
res[product.id] = product.list_price
File "/home/george/odoo/mattia_v7/openerp/osv/orm.py", line 503, in __getattr__
return self[name]
File "/home/george/odoo/mattia_v7/openerp/osv/orm.py", line 469, in __getitem__
elif field_column._type in ('one2many', 'many2many') and len(result_line[field_name]):
TypeError: object of type 'bool' has no len()
我可以在https://www.odoo.com/forum/help-1/question/can-i-store-field-function-with-the-type-one2many-356
上看到这似乎不受支持。 答案是否正确?有没有解决方法呢?
答案 0 :(得分:1)
尝试使用以下代码:
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')
答案 1 :(得分:1)
是的,存储x2many函数或计算字段完全没有意义。为什么?两种类型都需要一些特殊的设置,不能在字段定义上进行。在one2many上,你需要在另一个模型上使用相关的外键。在many2many上,您必须为两个模型键定义表。
我对odoo的经验表明:使用未存储的many2many函数字段(函数需要返回其通常字典中的id列表)。
另外,正如@Odedra在答案中提到的那样,你的功能并不正确。