Skip to Content
Menu
This question has been flagged
1 Reply
1151 Views

i want to set default value for one2many tree view here are my tables

class EstimationModel(models.Model):
        employee_id = fields.Many2one('res.users',string="Assign to",tracking=1)
        project_detail_ids = fields.One2many('project.details', 'estimation_id', string="Project Details")
        technology_id = fields.Many2one('technology.info',string="Technology Name",tracking=1)
        tag_ids = fields.Many2many('project.tag', string="Project Tags",store=True)
 

class ProjectDetails(models.Model):
    module_name = fields.Char(string="Module Name")
    hours = fields.Float(string="hours",related='task_id.hours',readonly=False)
    task_notes = fields.Text(string="Notes",related='task_id.task_notes',readonly=False)
    task_id = fields.Many2one('add.task', string='Module Name..')
    total_hours = fields.Float(string="Total hours",tracking=1)
    estimation_id = fields.Many2one('estimation.info', string="estimation")
    name = fields.Text(string='Notes', required=False)
    category_id = fields.Many2one('project.category',string='Category Name')

 
class AddTask(models.Model):
    technology_id = fields.Many2one('technology.info',string="Technology Name",tracking=1)
    tag_ids = fields.Many2many('project.tag', string="Project Tags")
    module_name = fields.Char(string="Module Name")
    hours = fields.Float(string="hours")
    total_hours = fields.Float(string="Total hours",tracking=1)
    estimation_id = fields.Many2one('estimation.info', string="estimation")
    name = fields.Text(string='Notes', required=False)
    notes = fields.Char(string='Add Notes')
    task_notes = fields.Text(string='Add Notes')

i want defult value technology vice on project_detail_ids tree view. we get module name , hours and notes from task_id many2one field. anyone have idea how can i set default value for project_detail_ids tree view


Avatar
Discard
Author

hello Ashish Thanks for your replay but you have any idea how we can make it dynamic like i want to filter data technology_id vice for example if user select python technology then it will display only python task and if user select php then it will display one php task

Best Answer

To set a default value for the project_detail_ids field in the EstimationModel model, you can use the default attribute in the field definition. For example, if you want to set the default value of project_detail_ids to be all the records in the AddTask model that have a technology_id value of 1, you can use the following code:

class EstimationModel(models.Model):
    # existing code here
    project_detail_ids = fields.One2many(
        'project.details',
        'estimation_id',
        string="Project Details",
        default=lambda self: self.env['project.details'].search([('task_id.technology_id', '=', 1)])
    )

This will set the default value of the project_detail_ids field to be all the project.details records that have a task_id value with a technology_id value of 1.

If you want to set the default value of the project_detail_ids field to be all the records in the AddTask model, you can use the following code instead:

class EstimationModel(models.Model):
    # existing code here
    project_detail_ids = fields.One2many(
        'project.details',
        'estimation_id',
        string="Project Details",
        default=lambda self: self.env['add.task'].search([])
    )

This will set the default value of the project_detail_ids field to be all the add.task records in the database.

Note that in both cases, the default value is a lambda function that searches for the relevant records in the database. This allows the default value to be dynamic and depend on the current context. For example,

Avatar
Discard