Pular para o conteúdo
Menu
Esta pergunta foi sinalizada
3 Respostas
1336 Visualizações

Hello I have two tables project.details and add.task  here add.task have m2o relation with  project.details in project details table we have tech_id and cat_id which is also m2o field so based tech_id and cat_id i want to select those value by defult on add.task table here tech_id and cat_id fields are also available .

anyone have any idea how can i give default selected value to tech_id and cat_id according to 

project.details tables values on create edit option

Avatar
Cancelar
Melhor resposta

​Hi, if you want to set default value of many2one you can assign "default" inside your field and create a method to it.

For example : 

def default_tech_id(self):
 tech_id
= self.env['project.details'].search([('your_field_inside_project_details', '=', 'your_value_inside_project_details')])
return operation_type.id

tech_id = fields.Many2one("project.details", default=default_tech_id)

Best regards,

Altela (altelasoftware.com)

Avatar
Cancelar
Melhor resposta

Hi, you can follow following link to know different ways to do this:

https://youtu.be/oMnHpHH54QU

Thanks

Avatar
Cancelar
Melhor resposta

Hi,

Utilize the default keyword argument to set the tech_id and cat_id fields in the add.task model. The default argument should be a function that returns the related record's ID from the project.details model.

Here is the code:

    tech_id = fields.Many2one('technology', string='Technology', default=_default_tech_id)
    cat_id = fields.Many2one('category', string='Category', default=_default_cat_id)
   
    def _default_tech_id(self):
        project_detail = self.env['project.details'].search([], limit=1)  # Get any project detail record
        return project_detail.tech_id.id if project_detail else False
   
    def _default_cat_id(self):
        project_detail = self.env['project.details'].search([], limit=1)  # Get any project detail record
        return project_detail.cat_id.id if project_detail else False

Hope it helps

Avatar
Cancelar