Odoo - 如何在另一个模块中读取/获取字段的值

时间:2016-02-15 13:36:21

标签: python python-2.7 openerp odoo-8

我在hr.employee模块中添加了一个新字段。

该字段为:'available_tickets': fields.float("Available Tickets")

我需要在另一个模块中读取这个字段的值,比如说travel.request模块。

但它不起作用,任何人都可以帮我解决它。

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 个答案:

答案 0 :(得分:2)

如果您只需要从many2one对象读取值,则可以使用相关字段。这里的例子:

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'),
    }

相关字段是 readonly 。 JFYI osv 已弃用(/odoo/openerp/osv/osv.py)。它会起作用,但您可以使用"compute" method and new API for models