Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
2 Antwoorden
8274 Weergaven

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
Annuleer
Beste antwoord
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
Annuleer
Beste antwoord

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
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
3
feb. 25
35121
0
okt. 23
1155
0
mrt. 15
3529
2
feb. 25
8429
1
sep. 24
2013