Skip to Content
Menu
This question has been flagged
2 Replies
8175 Views

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!

Avatar
Discard
Best Answer
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.
Avatar
Discard
Best Answer

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

Avatar
Discard
Related Posts Replies Views Activity
3
Feb 25
35031
0
Oct 23
1131
0
Mar 15
3500
2
Feb 25
8358
1
Sep 24
1954