manuthalasseril
manuthalasseril

Reputation: 1064

How to get the current user id (uid) in the form view of Odoo?

I want to add a domain for filtering current user id in the attrs attribute of button in the form view, which is given below :

<button name="my_name" attrs="{'invisible': [('my_field', '!=', uid)]}" />

Uid is not defined is the error. I tried with user.id, user_id., which also gives the same error. Please help me

Upvotes: 0

Views: 8732

Answers (3)

vijay maurya
vijay maurya

Reputation: 41

try this way

  1. create a Boolean field in ResUsers.
  2. create a boolean type compute field in class of your form.
    mark compute field True if Boolean field in ResUsers is True.
    if self.env['res.users'].browse(self.env.uid).show_button:
       self.computefield = True
  3. use compute field in attrs on button

Upvotes: 2

vijay maurya
vijay maurya

Reputation: 41

uid is only used within a domain, not within attrs.

Here is a workaround:

  1. create a group
  2. assign users to group
  3. assign group to button.
<button name="btn_name" type="object" string="string" groups="module.group_name"/>

Upvotes: 2

manuthalasseril
manuthalasseril

Reputation: 1064

In the view model, there is a python eval is execting for attrs attribute, thats why we are getting this error. For overcoming this we have to overide the function named "transfer_node_to_modifiers". The code is given below:

def transfer_node_to_modifiers(node, modifiers, context=None, in_tree_view=False):
if node.get('attrs'):
    #If you want, add more conditions here
    if ', uid' in  node.get('attrs'):
        user_id = str(context.get('uid', 1))
        user_id = ', ' + user_id
        attrs = node.get('attrs')
        attrs = attrs.replace(', uid', user_id)
        node.set('attrs', attrs)
    modifiers.update(eval(node.get('attrs')))

if node.get('states'):
    if 'invisible' in modifiers and isinstance(modifiers['invisible'], list):
        # TODO combine with AND or OR, use implicit AND for now.
        modifiers['invisible'].append(('state', 'not in', node.get('states').split(',')))
    else:
        modifiers['invisible'] = [('state', 'not in', node.get('states').split(','))]

for a in ('invisible', 'readonly', 'required'):
    if node.get(a):
        v = bool(eval(node.get(a), {'context': context or {}}))
        if in_tree_view and a == 'invisible':
            # Invisible in a tree view has a specific meaning, make it a
            # new key in the modifiers attribute.
            modifiers['tree_invisible'] = v
        elif v or (a not in modifiers or not isinstance(modifiers[a], list)):
            # Don't set the attribute to False if a dynamic value was
            # provided (i.e. a domain from attrs or states).
            modifiers[a] = v

Calling the uid is same as question

<button name="my_name" attrs="{'invisible': [('my_field', '!=', uid)]}" />

I created a patch for this. If anyone needed, please download from here

Upvotes: 1

Related Questions