콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
2 답글
2802 화면

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
1993
0
12월 19
4326
0
1월 20
3651
3
11월 23
17625
1
2월 24
2287