Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
1723 Lượt xem

I have a custom module and I have created a model and its view. I have an excel sheet that contains employees' work phones. I want when I import this excel sheet to get data of employees by matching the work phone in the sheet and the work phone in the hr.employee model. when I create a record manually by entering a work phone I get the data after I click on the save button, but when I import it I get nothing. I have created on change method for that here is my code: 

class BillBill(models.Model):
_name = "bill.bill"
_inherit = ['mail.thread', 'mail.activity.mixin']


employee_id = fields.Many2one('hr.employee', string='Employee', help="Employee")

work_phone = fields.Char(string='work_phone')

@api.onchange('work_phone')
def _onchange_work_phone(self):
    employee_rec = self.env['hr.employee']
    if self.work_phone:
        employee = employee_rec.search([('work_phone', '=', self.work_phone)])
        if not employee:
            raise UserError(_("No Employee by this Phone."))
        else:
            self.employee_id = employee.id


Ảnh đại diện
Huỷ bỏ
Tác giả

thank you very much you made my day


Câu trả lời hay nhất

You are using the onchange decorator which work only on form view so it will not be triggered when you import data as you can read below.

ORM API — Odoo 15.0 documentation

What you can do is to override the create method and write method (if needed).  

How to override Create method:

    
@api.model
def create(self, vals):
employee_rec = self.env['hr.employee']
if 'work_phone' in vals and vals.get('work_phone'):
employee = employee_rec.search([('work_phone', '=', vals.get('work_phone'))])
if not employee:
raise UserError(_("No Employee by this Phone."))
else:
vals['employee_id'] = employee.id
return super(BillBill, self).create(vals)


Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 6 23
2254
1
thg 1 23
2150
0
thg 9 22
1978
1
thg 8 22
2846
0
thg 8 22
2134