コンテンツへスキップ
メニュー
この質問にフラグが付けられました
2 返信
2667 ビュー

I want to know if it is possible for a Many2many field having the records automatically. I tried different things like a default lambda similar to many2one but Many2many didn't have the records.

アバター
破棄
最善の回答

Try with default method

@api.model
def _get_default_product_variant_ids
(self):
return self.env['product.product'].search([('type', '=', 'service')]).ids

product_variant_ids = fields.Many2many('product.product', string='Variants', default=_get_default_product_variant_ids)
アバター
破棄
著作者

Yes this worked! I have a question though, should a leaf or domain be mandatory?

Leaf or domain not mandatory, its all on your requirement what should you want to fill up, this given example to setup default value in which format needs only

著作者

@Krishna Bhalala yes that works too! much thanks!

最善の回答

Hi,

Try 

product_variant_ids = fields.Many2many(
'product.product',
string='Variants',
default=lambda self: self.env['product.product'].search([('can_be_expensed', '=', True)]).ids
)


アバター
破棄
著作者

It gives me an error
ValueError: Invalid field 'can_be_expensed' in leaf "<osv.ExtendedLeaf: ('can_be_expensed', '=', True) on product_product (ctx: )>"

Change domain as per your requirement,This error is because the field 'can_be_expensed' does not exist on the model.

関連投稿 返信 ビュー 活動
1
2月 23
1861
0
12月 19
4250
0
1月 20
3530
3
11月 23
17353
1
2月 24
2135