Odoo Help


User function inside domaine

on 6/19/17, 5:34 AM 258 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 :

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

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 ?

on 6/19/17, 8:22 AM

@cv, can you help me about this erreur please

NameError: global name 'etree' is not defined

on 6/19/17, 8:30 AM

@cv i got also this erreur :

Uncaught TypeError: Cannot read property 'arch' of null

on 6/19/17, 8:59 AM

Niyas Raphy

--Niyas Raphy--
--Niyas Raphy--

Odoo Techno Functionalist @ Cybrosys Techno Solutions

Graduation : B-Tech in Computer Science & Engineering from Calicut University (2012 - 2016)

Feel free to contact :  niyasraphyk@gmail.com

Niyas Raphy
On 6/19/17, 6:22 AM

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.


On 6/19/17, 7:57 AM

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")
On 6/19/17, 6:42 AM

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.

Ankit H Gandhi(AHG)
on 6/19/17, 6:53 AM

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?

on 6/19/17, 6:59 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 6/19/17, 5:34 AM
Seen: 258 times
Last updated: 6/19/17, 7:57 AM