Odoo Help

0

how to have checkbox depends on other fields?

By
Jérôme
on 10/21/15, 4:12 AM 561 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?

0
Axel Mendoza
On 10/21/15, 4:48 AM

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

 

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

Jérôme
on 10/21/15, 12:16 PM

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

Jérôme
on 10/21/15, 12:36 PM

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

Axel Mendoza
on 10/21/15, 1:24 PM

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)

Stats

Asked: 10/21/15, 4:12 AM
Seen: 561 times
Last updated: 10/21/15, 4:48 AM