This question has been flagged
5 Replies
1847 Views

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.


Avatar
Discard
Author

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

Author

@cv, can you help me about this erreur please

NameError: global name 'etree' is not defined

Author

@cv i got also this erreur :

Uncaught TypeError: Cannot read property 'arch' of null

Best Answer

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")
Avatar
Discard

Amazing Solution.

Best Answer

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


Avatar
Discard
Author Best Answer

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 :)


Avatar
Discard

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.

Author

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?