Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
2672 Lượt xem

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.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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)
Ảnh đại diện
Huỷ bỏ
Tác giả

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

Tác giả

@Krishna Bhalala yes that works too! much thanks!

Câu trả lời hay nhất

Hi,

Try 

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


Ảnh đại diện
Huỷ bỏ
Tác giả

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.

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 2 23
1865
0
thg 12 19
4257
0
thg 1 20
3535
3
thg 11 23
17371
1
thg 2 24
2147