Skip to Content
Menu
This question has been flagged

Hello community,


I'm trying to give a specific record to my new view that I call from an action.

I know how I can give the active_id and use that in the new model field as a default value.


I have a field as a Many2one of product.product. I want to give it a context from the 'product.template' view, but active_id should not be the 'product.template' ID, it should be the 'product.product' ID from that Base Product.


Something like this:

<field name="context">{'search_default_product_id': product_variant_id.id, 'default_product_id': product_variant_id.id}</field>

Thanks,





Avatar
Discard
Best Answer

Hi,

Can you achieve it using the the default_get method ? Super the default get method of the corresponding model and inside it, check whether you have particular value inside the context, if so, set the default value from there.

See a sample from the MRP module:

@api.model
def default_get(self, fields_list):
defaults = super(StockMove, self).default_get(fields_list)
if self.env.context.get('default_raw_material_production_id'):
production_id = self.env['mrp.production'].browse(self.env.context['default_raw_material_production_id'])
if production_id.state == 'done':
defaults['state'] = 'done'
defaults['product_uom_qty'] = 0.0
defaults['additional'] = True
return defaults

For more: Default Get Function: Set Default Values For Fields In Odoo


Thanks

Avatar
Discard
Author

thank you, I have fixed it with your example =)

Related Posts Replies Views Activity
1
Aug 19
3492
1
Jan 16
7594
3
Nov 24
5858
3
Sep 24
4401
0
Jun 21
1987