2016-11-11 113 views
0

關於樹狀圖記錄的點擊我想打開一個formview。當這個formview被打開時,我有兩個one2many字段需要動態填充(在我的情況下,我有一個產品列表,並點擊一個產品,我想搜索這個產品有多少庫存移動)Openerp v7 API函數字段'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

請用* _get_last_sales *方法更新您的問題。 –

+0

@Odedra更新了我的問題 –

回答

1

是,存儲x2many功能或計算領域毫無意義可言。爲什麼?這兩種類型都需要一些特殊的設置,不能在字段定義上進行設置。在one2many上,你需要在另一個模型上使用相關的外鍵。在很多情況下,您必須爲兩個模型鍵定義表格。

我對odoo的使用經驗顯示:使用未保存的many2many函數字段(函數需要返回其常用字典中的id列表)。

此外,您的功能不正確,因爲@Odedra在他的回答中提到過。

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') 
+0

請更具體一點,這段代碼做了什麼?我爲什麼要這樣做? –

+0

這段代碼將解決你的問題。在你的代碼中,有很多問題。例如,您沒有將值傳遞給特定的記錄ID。當我們使用* = *運算符時,我們需要設置適當的值。在你的情況下,你已經通過了名單。 *(customer_loc_id)*和*(production_loc_id)* –

+0

感謝您的時間和回覆@Odedra。但我的主要問題並不在於這些方法。我的主要問題是如何打開我的記錄的表單視圖時調用方法。請忽略這些方法我已經將它們刪除了,我只是插入它們以查看它們是否會被調用。 –