i have odoo17 project  and i need to show in form view of res.users the groups that i want based on users type and role, <?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <!-- Tree View -->
        <record id="view_user_tree_custom" model="ir.ui.view">
            <field name="name">res.users.tree</field>
            <field name="model">res.users</field>
            <field name="arch" type="xml">
                <tree string="Users" default_order="create_date desc">
                    <field name="name"/>
                    <field name="type"/>
                    <field name="role"/>
                    <field name="position"/>
                    <field name="login"/>
                    <field name="phone"/>
                </tree>
            </field>
        </record>
        <!-- Form View -->
        <record id="view_user_form_custom" model="ir.ui.view">
            <field name="name">res.users.form.custom</field>
            <field name="model">res.users</field>
            <field name="priority" eval="1"/>
            <field name="arch" type="xml">
                <form>
                    <header>
                        <button string="Set Password"
                                type="object" name="action_change_password"
                                class="btn-primary"
                                invisible="id == False"/>
                    </header>
                    <sheet>
                        <div class="oe_avatar_group">
                            <field name="image_1920" widget="image" class="oe_avatar"/>
                        </div>
                        <group>
                            <group>
                                <field name="name" required="1"/>
                                <field name="email" invisible="1"/>
                                <field name="login" required="1"/>
                                <field name="phone" placeholder="+998 xx xxx xx xx"/>
                                <field name="type" required="1"/>
                                <field name="position" invisible="type == 'admin'"/>
                                <field name="role" 
                                        invisible="type == 'admin'" 
                                        required="type != 'admin'"/>
                                
                                <field name="region_id" 
                                        invisible="type == 'admin' or role != 'region' and role != 'district' and role != 'mahalla'" 
                                        required="role in ['region', 'district', 'mahalla']"
                                        />
                                
                                <field name="district_id"
                                        invisible="type == 'admin' or role != 'district' and role != 'mahalla'" 
                                        required="role in ['district', 'mahalla']"
                                        domain="[('region_id', '=', region_id)]"/>
                                
                                <field name="mahalla_id" 
                                        invisible="type == 'admin' or role != 'mahalla'" 
                                        required="role == 'mahalla'"
                                        domain="[('district_id', '=', district_id)]"/>
                                <separator string="Access Rights" colspan="4" invisible="type == 'admin'"/>
                                <field name="groups_id" 
                                    widget="many2many_checkboxes" 
                                    options="{'no_create': True}"
                                    >
                                    <tree editable="bottom">
                                        <field name="name"/>
                                        <field name="category_id"/>
                                    </tree>
                                </field>
                            </group>
                        </group>
                    </sheet>
                </form>
            </field>
        </record>

        <!-- Kanban View -->
        <record id="view_user_kanban_custom" model="ir.ui.view">
            <field name="name">res.users.kanban</field>
            <field name="model">res.users</field>
            <field name="arch" type="xml">
                <kanban>
                    <field name="id"/>
                    <field name="image_1920"/>
                    <field name="name"/>
                    <field name="type"/>
                    <field name="role"/>
                    <field name="position"/>
                    <templates>
                        <t t-name="kanban-box">
                            <div class="oe_kanban_global_click o_kanban_record_has_image_fill o_res_partner_kanban">
                                <div class="o_kanban_image_fill_left d-none d-md-block"
                                    t-attf-style="background-image:url(#{kanban_image('res.users', 'image_128', record.id.raw_value)})"
                                    role="img"/>
                                <div class="oe_kanban_details">
                                    <strong class="o_kanban_record_title">
                                        <field name="name"/>
                                    </strong>
                                    <div>
                                        <strong>Type: </strong><field name="type"/>
                                    </div>
                                    <div t-if="record.role.raw_value">
                                        <strong>Role: </strong><field name="role"/>
                                    </div>
                                    <div t-if="record.position.raw_value">
                                        <strong>Position: </strong><field name="position"/>
                                    </div>
                                </div>
                            </div>
                        </t>
                    </templates>
                </kanban>
            </field>
        </record>

        <!-- Actions -->
        <record id="action_user_custom" model="ir.actions.act_window">
            <field name="name">Users</field>
            <field name="res_model">res.users</field>
            <field name="view_mode">form</field>
            <field name="view_id" ref="view_user_form_custom"/>
        </record>

        <record id="action_user_list" model="ir.actions.act_window">
            <field name="name">Users</field>
            <field name="res_model">res.users</field>
            <field name="view_mode">kanban,tree</field>
            <field name="view_id" ref="view_user_tree_custom"/>
        </record>

        <!-- Menus -->
        <menuitem
            id="user_menu"
            name="User Management"
            sequence="1"/>

        <menuitem
            id="user_managers_menu"
            name="Users"
            parent="user_menu"
            action="action_user_list"
            sequence="1"/>

        <menuitem
            id="user_add_menu"
            name="Add User"
            parent="user_menu"
            action="action_user_custom"
            sequence="2"/>
    </data>
</odoo>from odoo import fields, models, api
from odoo.exceptions import AccessError


class Users(models.Model):
    _inherit = "res.users"

    position = fields.Char(string='Position')
    type = fields.Selection([
        ('admin', 'Admin'),
        ('moderator', 'Moderator'),
        ('reader', 'Reader'),
    ], string='User Type', required=True, default='reader')
    
    role = fields.Selection([
        ('republic', 'Republic'),
        ('region', 'Region'),
        ('district', 'District'),
        ('mahalla', 'Mahalla'),
    ], string='Role', default='mahalla')
    
    region_id = fields.Many2one('region', string="Region")
    district_id = fields.Many2one('district', string="District")
    mahalla_id = fields.Many2one('mahalla', string="Mahalla")

    @api.onchange('type')
    def _onchange_type(self):
        if self.type == 'admin':
            self.role = False
            self.region_id = False
            self.district_id = False
            self.mahalla_id = False
        self._update_role_visibility()
    
    @api.onchange('role')
    def _onchange_role(self):
        if self.type != 'admin':
            self._update_role_visibility()
    
    def _update_role_visibility(self):
        if self.type == 'admin':
            pass
        elif self.role == 'republic':
            pass
        elif self.role == 'region':
            self.district_id = False
            self.mahalla_id = False
        elif self.role == 'district':
            self.mahalla_id = False
        elif self.role == 'mahalla':
            pass

    def action_change_password(self):
        self.ensure_one()
        return {
            'type': 'ir.actions.act_window',
            'res_model': 'change.password.wizard',
            'view_mode': 'form',
            'view_id': self.env.ref('base.change_password_wizard_view').id,
            'target': 'new',
            'context': {'default_user_id': self.id},
        }

    def assign_group_based_on_role(self):
        self.ensure_one()

        admin_group = self.env.ref('mahallam.group_admin')
        group_republic = self.env.ref('mahallam.group_republic')
        group_region = self.env.ref('mahallam.group_region')
        group_district = self.env.ref('mahallam.group_district')
        group_mahalla = self.env.ref('mahallam.group_mahalla')
        group_mahalla = self.env.ref('mahallam.group_mahalla')
        group_reader = self.env.ref('mahallam.group_reader')
        group_moderator = self.env.ref('mahallam.group_moderator')
        group_hierarchical = self.env.ref('mahallam.group_hierarchical')
        group_localization = self.env.ref('mahallam.group_localization')
        group_application = self.env.ref('mahallam.group_application')
        group_announcement = self.env.ref('mahallam.group_announcement')
        group_notification = self.env.ref('mahallam.group_notification')
        group_questionnaire = self.env.ref('mahallam.group_questionnaire')
        group_user_questionnaire = self.env.ref('mahallam.group_user_questionnaire')


        self.groups_id = [
            (3, admin_group.id), (3, group_republic.id), (3, group_region.id),
            (3, group_district.id), (3, group_mahalla.id),
            (3, group_reader.id), (3, group_moderator.id),
            (3, group_hierarchical.id), (3, group_localization.id),
            (3, group_application.id), (3, group_announcement.id),
            (3, group_notification.id), (3, group_questionnaire.id),
            (3, group_user_questionnaire.id),
            ]

        if self.type == 'moderator':
            self.groups_id = [(4, group_moderator.id)]
        elif self.role == 'republic':
            self.groups_id = [(4, group_republic.id)]
        elif self.role == 'region':
            self.groups_id = [(4, group_region.id), (4, group_localization.id), (4, group_hierarchical.id)]
        elif self.role == 'district':
            self.groups_id = [(4, group_district.id), (4, group_localization.id), (4, group_hierarchical.id)]
        elif self.role == 'mahalla':
            self.groups_id = [(4, group_mahalla.id), (4, group_localization.id), (4, group_hierarchical.id)]
        elif self.type == 'reader':
            self.groups_id = [(4, group_reader.id)]

    @api.model
    def create(self, vals):
        user = super(Users, self).create(vals)
        user.assign_group_based_on_role()
        return user

    def write(self, vals):
        res = super(Users, self).write(vals)
        if 'type' in vals or 'role' in vals:
            self.assign_group_based_on_role()
        return res