跳至內容
選單
此問題已被標幟
2 回覆
8320 瀏覽次數

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
35156
0
10月 23
1172
0
3月 15
3544
2
2月 25
8458
1
9月 24
2043