This question has been flagged
1 Reply
27871 Views

I created many2one field that has relationship custom model.I want to know how to self default value ?My default value is "Head/Branch".Here is my code.Thank You.

from odoo import models, fields, api

import logging

class CrmnNewTask(models.Model):

_inherit = 'res.partner'

head_branch=fields.Many2one('head.branch', string='Head/Branch', index=True, ondelete='cascade')

class Headbranch(models.Model):

_name='head.branch'

name=fields.Char('Head/Branch')


Avatar
Discard
Best Answer

Hi..

You need a record in head.branch named 'Head/Branch' or change the search accroding to your record.

class CrmnNewTask(models.Model):

     _inherit = 'res.partner'

    def _default_head_branch(self):

         return self.env['head.branch'].search([('name', '=', 'Head/Branch')], limit=1).id

     head_branch=fields.Many2one('head.branch', string='Head/Branch', index=True, ondelete='cascade', default=_default_head_branch)

Avatar
Discard
Author

Sorry Aslam. I don't clearly understand.I modified my code like this

class CrmnNewTask(models.Model):

_inherit = 'res.partner'

def _default_head_branch(self):

return self.env['head.branch'].search([('name','=','Head/Branch')],limit=1).id

head_branch=fields.Many2one('head.branch',string='Head/Branch',default=_default_head_branch)

class Headbranch(models.Model):

_name='head.branch'

name=fields.Char('Head/Branch')

I want my default value ('Head/Branch') in choose box.Like EUR in following link https://www.odoo.yenthevg.com/default-value-many2one-in-odoo-8/.Can you help me?Thank you.

head_branch is a many2one field. so it is expecting an id. you need to return the id of the default value you needed. that is all !

just give a print for this line 'self.env['head.branch'].search([('name', '=', 'Head/Branch')], limit=1).id' and check it is the id of the record "Head/Branch"