Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
2726 มุมมอง

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.

Related Posts ตอบกลับ มุมมอง กิจกรรม
1
ก.พ. 23
1913
0
ธ.ค. 19
4277
0
ม.ค. 20
3580
How to track One2many field in Odoo12? แก้ไขแล้ว
3
พ.ย. 23
17482
1
ก.พ. 24
2223