Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
13477 Lượt xem

I'm trying to get currently logged in user id similar to the usage of 

<field name = "domain_force"> [('manager.id', 'in', user.id)] </field>

I want to use it like this
attrs = "{'invisible': [('state', 'in', ['draft', 'approved', 'rejected']), ('manager.id', 'in', user.id)]} "
The above code is invalid as I cannot use it to get the current user so as to make it invisible.
It works in the case of domain but not in the case of attrs. 


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

UID In attrs are not supported. Instead of using the uid field in attrs you can use the computed field.

Solutions.
1. You can use a compute field to update the current user id and compare it in attrs

manager_id = fields.Integer(compute='get_current_uid', string='Manager ID Computed')

def get_current_uid(self):
"""

:param self:
:return:
"""
if self.env.context.get('uid', False):
self.manager_id = self.env.context.get('uid', False)
else:
self.manager_id = False
​attrs = "{'invisible': [('state', 'in', ['draft', 'approved', 'rejected']), ('manager.id', '=', computed_field)]}"


2. In ODOO this function(overrided and updated) allows getting uid in attrs.


# Don't deal with groups, it is done by check_group().
# Need the context to evaluate the invisible attribute on tree views.
# For non-tree views, the context shouldn't be given.
def transfer_node_to_modifiers(node, modifiers, context=None, in_tree_view=False):
if node.get('attrs'):
if 'uid' in node.get('attrs'):
if context.get('uid', False):
user_id = str(context.get('uid', False))
attrs = node.get('attrs')
node.set('attrs', attrs.replace('uid', user_id))
else:
user_id = '1'
attrs = node.get('attrs')
node.set('attrs', attrs.replace('uid', user_id))

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



Ảnh đại diện
Huỷ bỏ
Tác giả

This was really helpful. Thank you Hilar.

Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 10 20
3412
1
thg 1 24
15252
3
thg 8 22
4079
2
thg 12 21
6244
6
thg 8 20
7528