简体   繁体   中英

Get field value of inherited model Odoo 8

Hello to all I have been developing module under Odoo 8. I have a class "hrEmployee" with "_inherit=hr.employee" , now in my hrEmployee there is a One2many field having relation with another model "hr.employee.visa". I want to get the field values of the "hrEmployee" with onchange function defined on the field of "hr.employee.visa". Like when I change field value of "hrEmployee", I can get the field value entered on the current form (hrEmployee). How am I able to achieve this in Odoo v8? My Python code is shown below:

class hrEmployee(models.Model):

    _inherit = "hr.employee"               
    diwan_no = fields.Char('Diwan No', size=30, help='Diwan Number')   
    zeo_number = fields.Char('ZEO Number',size=30, help='ZEO Number')    
    visas_ids = fields.One2many('hr.employee.visas', 'employee_id', 'Visas')                    

class hr_employee_visas(models.Model):       

    _name='hr.employee.visas'
    employee_id = fields.Many2one("hr.employee.visas", "Employee" )

    @api.onchange('visas_number')    
    @api.depends( 'visas_number')    
    def _visa_num(self):    
        cr=self._cr    
        uid=self._uid    
        ids=self._ids
        for id in ids:
            obj1=self.pool.get('hr.employee').browse(cr,uid,id,context=None)  
            print obj1.name_related          

    visas_sponsor = fields.Char('Sponsor')    
    visas_states = fields.Selection([('apply','Apply'),('active','Active'),('expire','Expire'),('cancel','Cancelled')], string='State' )    
    visas_number = fields.Char('Visa No', help='Visa Number')   

I tried to use self.pool.get browse but it gives me "False" . Plz guide me or point me my mistake. Hopes for suggestion

Try following,

class hr_employee_visas(models.Model):       

    _name='hr.employee.visas'
    employee_id = fields.Many2one("hr.employee", "Employee" )

    @api.onchange('visas_number')    
    @api.depends( 'visas_number')    
    def _visa_num(self):    
        for obj in self:
            print obj.employee_id.name  

Here is the mistake

employee_id = fields.Many2one("hr.employee.visas", "Employee" )

You need to set hr.employee here.

No need to write both of the decorators together, in case of any changes into the visas_number field this method will be called, you can use any of the single decorator for this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM