2016-11-11 14 views
0

ツリービューレコードをクリックすると、フォームビューを開きます。そのフォームビューを開くと、私は2つの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。しかし、私の主な問題はこれらの方法にありません。私の主な問題は、レコードのフォームビューが開くときにメソッドを呼び出す方法です。私はすでに削除されているこれらのメソッドを無視してください、私はちょうど彼らが呼び出されるかどうかを確認するために挿入しました。 –

関連する問題