Friday 1 February 2019

How to Show Selection Values Based on Groups

I will be showing you how you can hide some values in your selection field to a group of users and to show the remaining values to another group of users. If you are an odoo developer, you already know how selection field works and if you are not an odoo developer, you are familiar with instances where you select from a drop down field and from a radio field, this is the way Odoo Selection field works also.
Selection field can be populated from another module but most times it is always predefined, sometimes you even extend an inherit selection field which is still predefined, here is what i mean:
fields.Selection([('service', 'Service'), ('product', 'Product')], string="Business Type")

You will realize that I already have service, product on the field and their is no way you can achieve what I will be explaining, so what do you do.

First Step: Define a Function

Defining a function can be done by anyone, but in our own case, when defining our function, you need to attach a decorator called @api.model to populate values on the field that calls the function
You will use @api.model as seen below:
@api.model
def _populate_choice(self):
    choices = [
        ('team_a', 'Team A'), ('team_b', 'Team B')
    ]
    return choices

Second Step: Call the Function

The next thing to do is to call the function on a field, and this should be a Selection field type:
sales_people = fields.Selection(string="Sales People", selection=_populate_choice, default='team_a')
You will notice that the method was called on the field and a default choice was set, that is optional though, but you should set the choices by calling the method on the attribute selection.

Third Step: Add a Condition

The aim of this tutorial is to show other choices based on a group you have already created.

And what you need to do is to check if the user model belong to a group, if the user belong to the defined group, then the other choices will be visible to the user, if not, it will be hidden.

This is how you do it:
@api.model
def _populate_choice(self):
    choices = [
        ('team_a', 'Team A'), ('team_b', 'Team B')
    ]
    if self.env['res.users'].has_group('module_name.group_id'):
        choices += [('team_c', 'Team C')]
    return choices

If you compare the method in Step #1 with this, you will notice that, I have conditioned the remaining choice, using the has_group. Now any user that belongs to the group will see "Team C"

Final Step: Make it Visible

All this we have done will be useless, if we can't see what we have done. So what you need to do is to add the field to your xml view just the way you add every other field.
<field name="sales_people" widget="radio"/>


I added the widget attribute because i want to see it in form of radio buttons, which i can remove, so the choice is yours.

Here is the result you should see, when the user is added to your group of choice and when the user is removed from the group.

User Belongs to Group



User Not in Group


I hope the guide was resourceful, kindly give your feedback as to what you feel or your experience working with selection fields

No comments:

Post a Comment