Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

in this my odoo17 view : <?xml version="1.0" encoding="utf-8"?> <odoo> <data> <!-- Tree…

  1. User

    in this my odoo17 view : res.users.tree res.users

        <!-- 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_tags" options="{'no_create': True}" invisible="type == 'admin'"/>
                            </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>
    

    i wanna add this view as part of my form for current user: view_name-ir.model.access.view.tree.edition, External ID-base.ir_access_view_tree_edition, Model Data-ir.model.access.view.tree.edition

  2. Assistant

    To embed the ir_access_view_tree_edition tree view into your custom res.users form view in Odoo 17, follow these steps:

    1. Understand the Objective:

      You want to integrate the existing tree view (ir_access_view_tree_edition) for model access rights into the current user’s form view. This typically involves displaying related records (like access rights) within the user form, allowing admin users to manage these rights directly from the user interface.

    2. Ensure a Relationship Exists:

      Note: The standard res.users model in Odoo manages access rights through groups (groups_id). If you intend to display or manage ir.model.access records directly, you need to establish a relationship between res.users and ir.model.access. This usually requires creating a custom field because, out of the box, users don't have a direct One2many or Many2many relationship with ir.model.access.

      Example Python Code:

      from odoo import models, fields
      
      class ResUsers(models.Model):
          _inherit = 'res.users'
      
          model_access_ids = fields.One2many(
              'ir.model.access', 
              'user_id',  # You need to add 'user_id' in 'ir.model.access' model
              string="Model Access Rights"
          )
      

      Important: By default, ir.model.access does not have a user_id field. You need to extend ir.model.access to include this field.

      Example Python Code for ir.model.access:

      from odoo import models, fields
      
      class IrModelAccess(models.Model):
          _inherit = 'ir.model.access'
      
          user_id = fields.Many2one('res.users', string="User")
      

      Security Consideration: Directly modifying access rights can have significant security implications. Ensure that only authorized users (like administrators) can modify these settings.

    3. Modify the Form View to Include the Tree View:

      Now, update your form view XML to embed the ir_access_view_tree_edition within the res.users form.

      Updated XML View:

      <?xml version="1.0" encoding="utf-8"?>
      <odoo>
          <data>
              <!-- Existing Tree, Form, and Kanban Views -->
              <!-- ... Your existing view definitions ... -->
      
              <!-- Updated Form View to Include Access Rights -->
              <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 not in ('region', 'district', 'mahalla')" 
                                          required="role in ('region', 'district', 'mahalla')"/>
                                      
                                      <field name="district_id"
                                          invisible="type == 'admin' or role not in ('district', '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_tags" options="{'no_create': True}" invisible="type == 'admin'"/>
                                  </group>
                              </group>
      
                              <!-- New Notebook to Include Access Rights Tree View -->
                              <notebook>
                                  <page string="Model Access Rights" attrs="{'invisible': [('type', '=', 'admin')]}">
                                      <field name="model_access_ids" context="{'default_user_id': id}">
                                          <!-- Reference the existing tree view -->
                                          <tree string="Access Rights" editable="bottom" decoration-info="user_id">
                                              <field name="name"/>
                                              <field name="model"/>
                                              <field name="perm_read"/>
                                              <field name="perm_write"/>
                                              <field name="perm_create"/>
                                              <field name="perm_unlink"/>
                                          </tree>
                                      </field>
                                  </page>
                              </notebook>
      
                          </sheet>
                      </form>
                  </field>
              </record>
      
              <!-- Actions and Menus -->
              <!-- ... Your existing action and menu definitions ... -->
      
          </data>
      </odoo>
      

      Explanation:

      • Notebook Integration: Added a <notebook> element with a new <page> labeled "Model Access Rights".
      • Embedding the Tree View: Inside the page, a <field> element (model_access_ids) is added. Within this field, a <tree> view is defined. While it's not possible to directly reference an external tree view by its ID within the <tree> tag, you can replicate its structure as shown.
      • Editable Mode: The editable="bottom" attribute allows inline editing of access rights directly from the form.
    4. Update ir.model.access with user_id:

      If you haven't already, ensure that each ir.model.access record is linked to a user. This can be done via the user_id field you added earlier.

      Example Python Code to Assign Access Rights: You might need to create a mechanism (like a server action or override the create method) to automatically assign user_id when access rights are created.

    5. Set Up Access Control:

      Ensure that only authorized users can view and modify the model_access_ids field. This involves updating security rules.

      Example Security Rule:

      <record id="group_access_manager" model="res.groups">
          <field name="name">Access Rights Manager</field>
          <field name="category_id" ref="base.module_category_hidden"/>
          <field name="implied_ids" eval="[(4, ref('base.group_system'))]"/>
      </record>
      
      <record id="ir_model_access_access_rights_manager" model="ir.model.access">
          <field name="name">access_rights_manager_access</field>
          <field name="model_id" ref="base.model_res_users"/>
          <field name="group_id" ref="your_module.group_access_manager"/>
          <field name="perm_read" eval="True"/>
          <field name="perm_write" eval="True"/>
          <field name="perm_create" eval="True"/>
          <field name="perm_unlink" eval="False"/>
      </record>
      
    6. Final Considerations:

      • Data Integrity: Direct manipulation of ir.model.access can lead to complex security configurations. Ensure data integrity by validating inputs and possibly adding constraints.
      • User Experience: Embedding detailed access rights within the user form can clutter the interface. Consider using smart buttons or separate dashboards if the access rights list is extensive.
      • Testing: Rigorously test the new form view to ensure that access rights are displayed and managed correctly without introducing security vulnerabilities.
    7. Alternative Approach:

      If your goal is to manage user groups and related access rights, leveraging Odoo’s existing group management system is recommended. You can enhance the groups_id field with additional widgets or functionalities without directly manipulating ir.model.access.

      Example Enhancement:

      <field name="groups_id" widget="many2many_tags" options="{'no_create': True}">
          <tree editable="bottom">
              <field name="name"/>
              <field name="category_id"/>
          </tree>
      </field>
      

      This allows managers to assign groups to users more intuitively, indirectly controlling access rights through group memberships.


    Summary: To embed the ir_access_view_tree_edition tree view into the res.users form, you need to establish a relationship between users and access rights, update the form view to include this related field with a tree representation, and ensure appropriate access controls are in place. Always proceed with caution when modifying access rights to maintain system security and integrity.