Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
2 Răspunsuri
2774 Vizualizări

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.

Imagine profil
Abandonează
Cel mai bun răspuns

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)
Imagine profil
Abandonează
Autor

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

Autor

@Krishna Bhalala yes that works too! much thanks!

Cel mai bun răspuns

Hi,

Try 

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


Imagine profil
Abandonează
Autor

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 Răspunsuri Vizualizări Activitate
1
feb. 23
1977
0
dec. 19
4320
0
ian. 20
3635
3
nov. 23
17572
1
feb. 24
2269