Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Employee to an other model

on 9/21/16, 5:32 AM 162 views

I want to put the employee name to the 'account.analytic.line' model.

For that I need to go to 'hr.payslip' and get the "number" field value. With this value I need to compare this value to the 'account.analytic.line' "ref" field value. So that I know to wich record what employee is set.


compare hr.payslip(number) with account.analytic.line(number)

Then get the employee name and put that in the account.analytic.line(testing) - new field.

This is my code:

from openerp import models, fields, api, osv
from openerp.http import requestfrom openerp import SUPERUSER_ID

class account_analytic_line(models.Model):
    _inherit = ['account.analytic.line']
     testing = fields.Integer(compute="employee")

     def employee(self):
         cr, uid, context, pool = request.cr, request.uid, request.context, request.cr
         model_obj = self.pool.get('hr.payslip')
         test = self.env['account.analytic.line']
         #record = model_obj.browse(cr, uid, your id, context = context)
         rec_ids = model_obj.search(cr, uid, [(test.ref, '=', 'number')], context=context)
         for record in model_obj.browse(cr, uid, rec_ids, context=context):
             print record.ref

The final result should be, that the employee should be on the account.analytic.line.

Martin Varela
On 9/21/16, 7:46 PM

Try this:

class account_analytic_line(models.Model):
    _inherit = ['account.analytic.line']   
testing = fields.Char(compute='_get_employee')
def _get_employee(self):
        for record in self:
            payslip_obj = self.env['hr.payslip']
payslip = payslip_obj.search([('number', '=', record.ref)], limit=1)
if payslip:
                record.testing = payslip.employee_id.name
                record.testing = False

amazing thank you !

on 9/22/16, 2:54 AM

how can I put this on the tree view ?

on 9/22/16, 2:56 AM


Expected singleton: account.analytic.line(3713, 3714, 3715, 3755, 3873, 3746, 3747, 3748, 3749, 3750, 3879, 3752, 3881, 3882, 3883, 3837, 3786, 3787, 3788, 3789, 3790, 3791, 3792, 3793, 3794, 3875, 3876, 3877, 3835, 3878, 3751, 3831, 3880, 3795, 3829, 3830, 3753, 3832, 3833, 3834, 3874, 3836, 3754, 3838, 3839)

on 9/22/16, 5:51 AM

I changed the code:

record.ref (before was self.ref that was wrong)

record.testing (before was self.testing that was wrong)

Martin Varela
on 9/22/16, 2:38 PM

thank you Martin! what was the problem before?

on 9/23/16, 4:33 AM

self is the entire recordset, in tree view are all the lines. In form view worked because it´s only one record. The correct way is to define a loop for each record (for record in self:) and calculate it´s value. The previous answer was wrong because in your tree view self was account.analytic.line(3713, 3714, 3715, 3755, 3873,...) and you can´t ask for a value of multiple records at one time (self.ref).

Martin Varela
on 9/23/16, 9:03 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 9/21/16, 5:32 AM
Seen: 162 times
Last updated: 9/22/16, 2:54 AM