跳至内容
菜单
此问题已终结
8 回复
7755 查看

HI,

I have 3 field in my view

                                <field name="name" />
                                <field name="date" />
                                <field name="nbc" />

I want set name and date field read only for hr_manager. how can i ?

Any help ??

 

 

形象
丢弃
最佳答案

Create function field that returns a value based on the user's group and then use attrs attributes on fields to control.

Reference :https://answers.launchpad.net/openobject-server/+question/178779

形象
丢弃
编写者

Thanks

最佳答案

Hi, you can use this as example...

'name': fields.char('Name', size=128, read=['base.group_userA'], write=['base.group_userB'] )

Read will allow the user group to view and Write will allow the user group to edit the field..

形象
丢弃
编写者

I have try this but its not working

最佳答案

*EDITED*

One solution is to combine a function field with the attrs tag:

.py:

def _get_uid_is_manager(self, cr, uid, ids, field_name, arg, context=None):
        res = {};
        manager_group_id = self.pool.get('ir.model.data').get_object(cr, uid, 'base', 'group_hr_manager').id
        manager_ids = self.pool.get('res.user').search('groups_id', '=', manager_group_id);
        for obj in self.browse(cr, uid, ids, context=context):
            res[obj.id] = uid in manager_ids;
        return res;

'uid_is_manager': fields_function(_get_uid_is_manager, type='boolean', method=True, string='Current User is Manager?'

.xml:

<field name="name" attrs="{'readonly': [('uid_is_manager' ,'=', True)]}"/>
<field name="date" attrs="{'readonly': [('uid_is_manager' ,'=', True)]}"/>

 

Regards.

形象
丢弃
编写者

@René Schuster: Thanks for the answer, I want make read only dont want hide.By using this code its hide for other user.

Sorry, got you wrong. I've edited my answer (but was slower than prakash ^^).

最佳答案

Use fields_get to achieve this:

Eg. 

@api.model
def fields_view_get(self, view_id='OdooModule.test_form_view', view_type='form', toolbar=False, submenu=False):
res = super(Test, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar,
submenu = submenu)
doc = etree.XML(res['arch'])
if not self.env.user.has_group('group_admin') or self.env.user.has_group('group_ad_doctor'):
for node in doc.xpath("//field[@name='laboratory']"):
node.set('modifiers', '{"readonly": true}')
for node in doc.xpath("//field[@name='test_type_id']"):
node.set('modifiers', '{"readonly": true}')

for node in doc.xpath("//field[@name='patient_id']"):
node.set('modifiers', '{"readonly": true}')


res['arch'] = etree.tostring(doc)
return res

形象
丢弃
相关帖文 回复 查看 活动
1
10月 24
2264
9
7月 21
62903
3
8月 24
15867
0
5月 23
1708
1
3月 23
2317