Odoo - Default value for a many2one field?

2.3k Views Asked by At

In Odoo, I wanted to set default fields which are filled by users for a many2one field. I can set default values for many2one field basically with default= function, but it's filled by me not by the users in the server. Here's my example:

class ProjectReport(models.Model):
    _name = 'project.report'
    _rec_name = 'project_name'

    project_name = fields.Char(string='Project Name', tracking=True)
    project_duration = fields.Float(string='Project Duration', tracking=True)
   

and my second class:

class ApprovalRequestWizard(models.TransientModel):
    _name = 'approval.request.wizard'
    _description = 'Approval Request Wizard'


    
    responsible_name = fields.Char(string='Responsible',
                                    default=lambda self: self.env.user.name)

    project_name = fields.Many2one('project.report', string='Inherited Project Name', default=lambda self: self.project_name.id)

    project_duration = fields.Float(string='Project Duration (h)', related='project_name.project_duration', tracking=True)

In this way, when the user select project name from many2one field, project duration values gets by itself. But I want project name gets by itself also.

When the user created any project, I would like to have button called "Approval" which triggers wizard class filled by automatically. How can I do that ? Thanks in advance.

1

There are 1 best solutions below

0
Koray Kupa On

In XML File, instead of:

context="{'default_project_name': active_id}

I use:

context="{'default_project_name': project_name}

and it's worked.

P.S. project_report field has to be Char in this type