Skip to Content
Meniu
Trebuie să fiți înregistrat pentru a interacționa cu comunitatea.
Această întrebare a fost marcată
5 Răspunsuri
2375 Vizualizări

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.


Imagine profil
Abandonează
Autor

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

Autor

@cv, can you help me about this erreur please

NameError: global name 'etree' is not defined

Autor

@cv i got also this erreur :

Uncaught TypeError: Cannot read property 'arch' of null

Cel mai bun răspuns

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")
Imagine profil
Abandonează

Amazing Solution.

Cel mai bun răspuns

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


Imagine profil
Abandonează
Autor Cel mai bun răspuns

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


Imagine profil
Abandonează

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.

Autor

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?