Hello,
I am trying to set some default values, but i can't figure it out. I have added a many2one field to the Product model, which i would like to have the default value of default_code (internal reference) which is a char field. In the _defaults i call the _get_default_code function, where i assume i have to first find the current product and get its default code (i don't know how to do this), then create a new product.code with this value, then return this new product.code. This is what i have so far:
class product_code(osv.osv):
_name = 'product.code'
_columns = {
'name': fields.char('Name', required=True, translate=True),
}
class product_legacy(osv.osv):
_inherit = 'product.template'
_columns = {
'default_code': fields.many2one('product.code', 'Internal Reference'),
}
def _get_default_code(self, cr, uid, context=None):
#get current product's default code
#res = self.pool.get('product.template').search(???)
code_obj = self.pool.get('product.code')
code_obj.create(cr, uid, {
'name': res['name']
}, context=context)
return code_obj
_defaults = {
'default_code':_get_default_code,
}
Even if i put a test value instead of "res['name']" it doesn't do anything. When i create a new product or edit an existing product, the many2one field is still empty.
Thank you for the help.
https://www.odoo.com/es_ES/forum/help-1/question/set-default-value-in-object-of-many2one-field-39425