跳至内容
菜单
此问题已终结
1 回复
4075 查看

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.

step:


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")

     @api.one
     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):
             record.ref
             print record.ref


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


形象
丢弃
最佳答案

Try this:


class account_analytic_line(models.Model):
    _inherit = ['account.analytic.line']   
testing = fields.Char(compute='_get_employee')
   
@api.depends('ref')
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
else:
                record.testing = False


形象
丢弃
编写者

amazing thank you !

编写者

how can I put this on the tree view ?

编写者

ValueError

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)

I changed the code:

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

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

编写者

thank you Martin! what was the problem before?

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).

相关帖文 回复 查看 活动
1
9月 16
4344
0
12月 16
3040
1
6月 24
1853
0
3月 23
2458
1
12月 20
2482