Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
3 Replies
1283 Tampilan
Hi, I am a beginner in odoo. I need to create a new module values  come ​​from the hr.employee .add button in hr.employee when click the buttons it  save same data like 'name,email..' .but when change the data it create new record in my new module don't update . how can I update the data in the new module without creating a new record? This is the function
def action_create_custom_profile(self):
for employee in self :
custom_record = self.env['custom.model'].search([('employee_id', '=', employee.id)], limit=1)

if not custom_record :
custom_record_vals = {
'name' : employee.name,
'gender' : employee.gender,
'mobile' : employee.mobile_phone,
'image' : employee.image_128,
'practice_area_ids' : employee.practice_area_ids,
'email' : employee.work_email,
'copmany_id' : employee.company_id,
'active' : employee.active,
# Add more fields as neede
}
self.env['custom.model'].create(custom_record_vals)
else :
# Update existing record
custom_record.write({
'name' : employee.name,
'gender' : employee.gender,
'mobile' : employee.mobile_phone,
'image' : employee.image_128,
# Add more fields as needed
})


 


Avatar
Buang
Jawaban Terbai

Hello, 


    To identify the issue that you have mentioned. let's update the code in 

    'action_create_custom_profile' method.

      

    def action_create_custom_profile(self):

    for employee in self:

        # Search for an existing custom record linked to this employee

        custom_record = self.env['custom.model'].search([('employee_id', '=', employee.id)], limit=1)


        # Prepare the values to be created/updated

        custom_record_vals = {

            'name': employee.name,

            'gender': employee.gender,

            'mobile': employee.mobile_phone,

            'image': employee.image_128,

            'practice_area_ids': [(6, 0, employee.practice_area_ids.ids)],  # Many2many field update

            'email': employee.work_email,

            'company_id': employee.company_id.id,  # Note: 'company_id' not 'copmany_id'

            'active': employee.active,

            # Add more fields as needed

        }


        if not custom_record:

            # Create a new custom record

            custom_record_vals['employee_id'] = employee.id  # Ensure the relationship is established

            self.env['custom.model'].create(custom_record_vals)

        else:

            # Update the existing custom record

            custom_record.write(custom_record_vals)

I Hope this information proves helpful to you.

Thanks & Regards,

Email:   odoo@aktivsoftware.com           

Skype: kalpeshmaheshwari 

Avatar
Buang
Jawaban Terbai

Hi,

Use the following code to avoid creating a new record when we change the value of an existing record by ensuring the employee_id field is set in your custom model, creating a link between the two models.

def action_create_custom_profile(self):

    for employee in self:

        custom_record = self.env['custom.model'].search([('employee_id', '=', employee.id)], limit=1)


        custom_record_vals = {

            'employee_id': employee.id,  # Add this line to link the record

            'name': employee.name,

            'gender': employee.gender,

            'mobile': employee.mobile_phone,

            'image': employee.image_128,

            'practice_area_ids': [(6, 0, employee.practice_area_ids.ids)],  # Use this format for many2many fields

            'email': employee.work_email,

            'company_id': employee.company_id.id,  # Use .id for many2one fields

            'active': employee.active,

        }


        if not custom_record:

            self.env['custom.model'].create(custom_record_vals)

        else:

            custom_record.write(custom_record_vals)

    return True


Hope it helps.

Avatar
Buang
Jawaban Terbai

Hi,
You have to add a field employee_id many2one field to hr.employee in the model "custom.model"


Then during the creation of the record pass value to this field:


'employee_id': employee_id


Thanks

Avatar
Buang