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

Hello, how do i set the default value for my many2one field. i have this code but it returns and error.


my_field = fields.many2one(

​comodel_name="module.i.want",

​string="My many2one field",

​default= lambda self: self.env['module.i.want'].search([('code', '=', 'model_code_1')], limit=1).id

)


I checked the type of the highlighted code and it returns a type 'function' not an 'integer' (which is the error):

psycopg2.errors.InvalidTextRepresentation: invalid input syntax for type integer: "module_code_1"


Thanks for the help!

形象
丢弃
最佳答案
Hello,

Please remove the id after the search method. Try with this

my_field = fields.many2one(
​comodel_name="module.i.want",
​string="My many2one field",
​default= lambda self: self.env['module.i.want'].search([('code', '=', 'model_code_1')], limit=1)
)


Please check the type of the code field from the module you search.
Hope it will help you.
形象
丢弃
最佳答案

Hi,

You can use the default function ,check with this example code snippet

def _default_partner(self):
return self.env['res.partner'].search([('name', '=', 'NAME OF THE PARTNER')], limit=1).id

partner_id = fields.Many2one('res.partner', string='Customer Name', default=_default_partner)

Regards

形象
丢弃
相关帖文 回复 查看 活动
3
2月 25
35238
0
10月 23
1193
0
3月 15
3566
2
2月 25
8493
1
9月 24
2090