Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odgovori
29441 Prikazi

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
Opusti
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
Opusti
Avtor

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"

Related Posts Odgovori Prikazi Aktivnost
2
jul. 25
4818
2
dec. 24
7905
2
nov. 24
28712
2
maj 24
7598
3
mar. 24
7051