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

Good morning,

I have many2one field,  and i would like to show it only to users who are admin.

So i have created a function which return the groupe name of connected user :

@api.multi
def user_group(self,cr,uid,ids,context):
user = self.env['res.users'].browse(self.env.uid)

if user.has_group('gestion_parcelles.group_ferme_admin'):
return('admin')

elif user.has_group('gestion_parcelles.group_ferme_manage'):
return ('manager')

elif user.has_group('gestion_parcelles.group_ferme_user'):
return ('user')

Then i have used it to filter my field :

approved_by = fields.Many2one('res.users', 'Affecter à', domain="[('user_group()', '=', 'admin')]")

But in the execution i get an error; can you show me how i should use thid function in my domaine filter.


形象
丢弃
编写者

Thank you @CV, and how then i will call: " def fields_view_get " function ?

编写者

@cv, can you help me about this erreur please

NameError: global name 'etree' is not defined

编写者

@cv i got also this erreur :

Uncaught TypeError: Cannot read property 'arch' of null

最佳答案

Hello  Zakaria,

To apply User Group on a field through from py file there is a way:

you have to Overwrite fields_view_get method.

def fields_view_get(self, view_id=None, view_type='form',
toolbar=False, submenu=False):
ret_val = super(ObjectName, self).fields_view_get(
view_id=view_id, view_type=view_type,
toolbar=toolbar, submenu=submenu)

doc = etree.XML(ret_val['arch'])

for field in ret_val['fields']:
for node in doc.xpath("//field[@name='%s']" % field):
node.set("groups", "module_name.group_name")
形象
丢弃

Amazing Solution.

最佳答案

Hi Zakaria,

Are you trying to make the many2one field visible only for the admin level users ? If so the better option is to give the groups to the many2one field in the XML,

<field name="field_name" groups="module_name.group_name" />

so that the field will be visible only for the admin level users.

Thanks


形象
丢弃
编写者 最佳答案

Thank you so much my friend it work,

But if i want to apply this conidtion from the python file not from the xml, how it will be so ?

Thank you so much :)


形象
丢弃

Hello Zakaria,

Kindly create one boolean field and set value based on logged user is admin then set true else set false.

After use attrs on many2one field, Using attrs you can visible field only admin user.

编写者

Hi Ankit,

Thank you for your help. Do you have a small example for that ?

If i have other type of users not only the Admin, what i should do in this case?