Search code examples
pythonodooodoo-12

Adding ManyTomany list in users view (Odoo 12)


This is the use case:

  • Allowing certain users to have access to specific budgets according to their permissions.

Logic:

  • I created a relationship ManytoMany between two models: crossovered.budget and res.users the models are the following:

    class res_users(models.Model):
        _inherit = 'res.users'
        budgets = fields.Many2many('crossovered.budget',
                                             string='Budget permissions')
    
    class crossovered_budget(models.Model):
        _inherit = 'crossovered.budget'
        res_users = fields.Many2many('res.users','Users')
    

I've successfully added the properties in both models with no problems, the issue is that I want to modify the user's view to display and select the budgets to each user but I can't get the widget (or field) to display in the form, I don't get any errors and my module installs without any issues.

this is the view:

<?xml version="1.0" encoding="UTF-8"?>
<odoo>
    <data>
        <record model="ir.ui.view" id="userscustom">
            <field name="name">Budget list</field>
            <field name="model">res.users</field>
            <field name="secuence">99</field>
            <field name="inherit_id" ref="base.view_users_form"/>
            <field name="mode">primary</field>
            <field name="view_mode">tree,form</field>
                <field name="arch" type="xml">
                <xpath expr="//notebook/page[@name='access_rights']/group" position="after">
                    <group name="rw_budget_group">
                        <field name="budgets" string="Allowed Budgets" widget="many2many_tags" options="{'no_create': True}" />
                    </group>
                </xpath>
            </field>
        </record>
    </data>
</odoo>

As I understand the group should be added after the node described in the XPath, Any help would be appreciated.


Solution

  • View inheritance mode in Odoo can be done in two types of mode, primary and extension. Primary mode view inheritance creates a totally new view which can be used as a separate view while extension which is default value if not mentioned, will add inheritance to the existing view. Either change inheritance mode to extension or bind your userscustom view to a new action to keep the original functionality unchanged.

    <field name="mode">extension</field>