Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odpovědi
8266 Zobrazení

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
Zrušit
Nejlepší odpověď
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
Zrušit
Nejlepší odpověď

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
Zrušit
Related Posts Odpovědi Zobrazení Aktivita
3
úno 25
35105
0
říj 23
1151
0
bře 15
3527
2
úno 25
8422
1
zář 24
2003