ZueZue
ZueZue

Reputation: 17

How to set default value to Many2one field in odoo10?

I created a 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')

Upvotes: 0

Views: 2924

Answers (2)

Bhoomi Vaishnani
Bhoomi Vaishnani

Reputation: 716

  • Please implement this example in your code :

    user_id = fields.Many2one('res.users','User', default=lambda self: self.env.user)

  • Here I have set current user name in many2one field. You can also set default value using function. This one another example :

*

tax_group_id = fields.Many2one('account.tax.group', string="Tax Group", default=_default_tax_group, required=True)

@api.model
def _default_tax_group(self):
    return self.env['account.tax.group'].search([], limit=1)

*

Upvotes: 2

user_odoo
user_odoo

Reputation: 2358

Try this:

  1. Go to your form where is field head_branch

  2. Active developer mode

  3. Populate field and save as default https://i.sstatic.net/gHPS3.jpg

Upvotes: 0

Related Questions