This question has been flagged
2 Replies
15931 Views

Hello everyone,

My doubt is what I am trying to do is, I have one2many field named `salary_ids` in my module like this :


salary_ids = fields.One2many('team.salary', 'employee_id', 'Salaries')

And inverse model employee_id like this:

class Salary(models.Model):
_name = 'team.salary'
_description = 'Salary'

month = fields.Selection([(str(ele), str(ele)) for ele in range(1, 13)], 'Month')
basic = fields.Float('Basic', tracking=True)
allowance = fields.Float('Allowance')
deduction = fields.Float('Deduction', tracking=True)
employee_id = fields.Many2one('team.team', 'Employee')
# employee_id = fields.Many2one('team.team', 'Employee', ondelete='cascade')
gross_sal = fields.Float('Gross', compute='_calc_net_gross')
net_sal = fields.Float('Net', compute='_calc_net_gross')

Now I want to add new records using wizard for this one2many fields so that when I select new month new basic and all it will added to that particular employees. I am not trying to update existing records but add new records to it. How can I?? 

Below is what I have tried so for using wizard.


class UpdateSalary(models.TransientModel):

_name = 'update.sal'
_description = 'Update Salaries for Particular employee'

salary_id = fields.Many2one('team.salary')
# salary_ids = fields.One2many('team.salary', 'employee_id', 'Salaries')
employee_id = fields.Many2one('team.team', 'Employee')
month = fields.Selection([(str(ele), str(ele)) for ele in range(1, 13)], 'Month')
basic = fields.Float('Basic', tracking=True)
allowance = fields.Float('Allowance')
deduction = fields.Float('Deduction', tracking=True)

def update_sal(self):
"""
Add salary record for particular employee
"""
print("salary_id: ", self.salary_id)
print('self.env.context', self.env.context)

??

Avatar
Discard
Best Answer

Hello,

First you need to create a record of team.salary from the wizard and then add that record into one2many field of respective model.

Please look the below code.

def update_sal(self):

    """

    Add salary record for particular employee

    """

    team_salary_id = self.env['team.salary'].create({

        'employee_id': self.employee_id,

        'month': self.month,

        'basic': self.basic,

        'allowance': self.allowance,

        'deduction': self.deduction,

    })

    # now you need to add above created team_salary record in respective model where you need to update its one2many(i.e salary_ids) field.

    self.env['your_model'].write({'

        'salary_ids': [(4, team_salary_id.id)] # link to existing record with id = ID (adds a relationship)

    '})

All the Best,

Thanks.

Avatar
Discard
Best Answer

These are the flags which is used for updating Odoo One2many or Many2many fields.

(0, 0, { values }) link to a new record that needs to be created with the given values dictionary

(1, ID, { values }) update the linked record with id = ID (write values on it)

(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)

(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)

(4, ID) link to existing record with id = ID (adds a relationship)

(5) unlink all (like using (3,ID) for all linked records)

(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)

Here is an example that updates seller one2many with details.


self.product_service.write({'seller_ids': [
(0, 0, {'name': self.asustec.id, 'product_code': 'ASUCODE'}),
(0, 0, {'name': self.camptocamp.id, 'product_code': 'C2CCODE'}),
]})


Avatar
Discard