跳至内容
菜单
此问题已终结
1 回复
7131 查看

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,





形象
丢弃
最佳答案

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

形象
丢弃
编写者

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

相关帖文 回复 查看 活动
1
8月 19
4198
1
1月 16
8749
3
5月 25
1279
1
4月 25
3145
3
11月 24
8013