2

I've added a new field inside hr.employee module .

The field is : 'available_tickets': fields.float("Available Tickets")

I need to read this field's value in another module let's say travel.request module.

But it didn't work, can anyone help me to fix it.

class travel_request(osv.osv):
    _name = 'travel.request'

    def _get_available_tickets(self, cr, uid, context=None):
        ticket_obj = self.pool.get('hr.employee')
        ticket_ids = self.pool.get('hr.employee').search(cr, uid, [])
        for t_id in ticket_ids:
            ticket = ticket_obj.browse(cr, uid, t_id, context=context)
        available_tickets = ticket.available_tickets
        return available_tickets 

     _columns = {
         'available_tickets_id': fields.many2one('hr.employee', "Number of Available Tickets", readonly=True),
     }
     _defaults = {
         'available_tickets_id': _get_available_tickets,
     }


class hr_employee(osv.osv):
    _inherit = "hr.employee"
    _columns = {
        'available_tickets': fields.float("Available Tickets"),
     }
1
  • What error do you face? Do you get any stacktrace in the server logs? That can help people to answer to your question.
    – pna
    Feb 15, 2016 at 14:20

1 Answer 1

2

If you need just read value from many2one object you can use related field. Here example:

class hr_employee(osv.osv):
    _inherit="hr.employee"
    _columns = {
        'available_tickets': fields.float("Available Tickets"),
    }


class travel_request(osv.osv):
    _name = 'travel.request'
    _columns = {
        'employee_id': fields.many2one('hr.employee', 'Employee',),
        'employee_tickets': fields.related('employee_id', 'available_tickets', string='Available Tickets', type='float'),
    }

Related fields is readonly. JFYI osv is deprecated (/odoo/openerp/osv/osv.py). It is will be work, but you can use "compute" method and new API for models.

2
  • Thank you, that's exactly what I need :D
    – youta
    Feb 16, 2016 at 7:46
  • @youta no problem. Good luck ;) Feb 16, 2016 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.