Skip to Content
Menu
This question has been flagged
1 Reply
3970 Views

Hello

I would like to have a checkbox on my app employee, and I would like this check box is check or uncheck on depends on 2 other fields!


explication:

on my form i have two fields :

'user_id' (the user relative to the employee)

'x_user_id' (a personal field and it's the id of the connected user)

So if the connected user is the same of the employee in the view i want my checkbox is uncheck

else if the connected user is different of the employee in the view i want my checkbox is check.


I try to create à boolean field with a compute arg !!! but i don't know how to write this in the compute cells!

I try to create a simple module like this but it doesn't work !!!

from openerp import SUPERUSER_ID
from openerp.osv import fields, osv
class hremployee(models.Model):
 _name = "hr.employee"
 _description = "Employee"
 _inherit = "hr.employee"
def _get_employee_user(self, cr, uid, ids, field_name, args, context=None):
 if context is None:
 context = {}
 if isinstance(ids, (int, long)):
 ids = [ids]
 res = {}
 for emp in self.browse(cr, uid, ids, context=context):
 res[emp.id] = True
 if emp.user_id == uid:
 res[emp.id] = False
 return res
 invisible = fields.Boolean(compute='_get_employee_user')


Can you help me?

Avatar
Discard
Best Answer

Try this way in the old api (you had defined to import osv instead of models)

from openerp.osv import fields, osv

class hremployee(osv.osv):
   _inherit = "hr.employee"

    def _get_employee_user(self, cr, uid, ids, field_name, args, context=None):
       if context is None:
           context = {}
       if isinstance(ids, (int, long)):
           ids = [ids]
       res = {}
       for emp in self.browse(cr, uid, ids, context=context):
           if emp.user_id == uid:
               res[emp.id] = False
            else:
    res[emp.id] = True
       return res

    _columns = {
       'invisible': fields.function(_get_employee_user, type='boolean', string='Invisible')
    }

Or this way in the new api:

from openerp import models, fields, api

class hremployee(models.Model):
_inherit = "hr.employee"

@api.multi
   def _get_employee_user(self):
for emp in self:
if emp.user_id == uid:
emp.invisible = False
            else:
               emp.invisible = True

invisible = fields.Boolean(compute='_get_employee_user')

 

Avatar
Discard
Author

hello axel and thanks for you're help. i try with the new api, the install is ok and the fields invisible was created in model employee, but after adding this fields in the form i had an error when i try to view my data: File "/usr/lib/python2.7/dist-packages/openerp/addons/hr_inv/hr_inv.py", line 10, in _get_employee_user if emp.user_id == uid: NameError: global name 'uid' is not defined

Author

I change if emp.user_id == uid: by if emp.user_id == emp.x_user_uid: and i have no errors but my check box is always check!!! the first if is never verified!!!! i don't understand

Yes, I only rearrange your code in the way that should work, I don't check your business rules