Check if specific group(s) are in context - Pyson

Using pyson, I can set the states attribute for a field based on the context.

As an example, if the number of groups in context is greater than 2, the following will set a field to be read-only:

description = fields.Char('Description', 
           states={'readonly': Len(Eval('context', {}).get('groups') ) > 2  } )

Now, suppose I want to allow the above field to be read-only if the group with id = 20 is in groups.

I tried using: 'readonly': In(20, Eval('context', {}).get('groups') ) , but that gave the error:

File "C:\Users\user\.virtualenvs\tryton_test\lib\site-packages\trytond\", line 485, in __init__
    assert obj.types().issubset({dict, list}), \
AssertionError: obj must be a dict or a list

I also tried: 'readonly': 20.in_(Eval('context', {}).get('groups') ), but that gave the error:

 File "C:\Users\user\.virtualenvs\tryton_test\lib\site-packages\trytond\modules\sale\", line 137
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

Where am I going wrong?


Is there a reason why you cannot just remove write access for this field on this group ?

The type of groups must be a list or a dict. PYSON use the default value (which is an empty string) to determine the type. So you must explicitly say that groups is a list with: Eval('context', {}).get('groups', []).


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.