Reputation: 1064
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
Reputation: 41
try this way
Upvotes: 2
Reputation: 41
uid is only used within a domain, not within attrs.
Here is a workaround:
<button name="btn_name" type="object" string="string" groups="module.group_name"/>
Upvotes: 2
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