tom10271
tom10271

Reputation: 4649

Odoo - How to update non updateable records by XML

I have created a few companies under res.company

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data noupdate="True">
        <record id="partner_my_company_hk" model="res.partner" context="{'default_is_company': True}">
            <field name="name">My company HK</field>
            <field name="company_id" eval="None"/>
            <field name="customer" eval="False"/>
            <field name="is_company" eval="True"/>
            <field name="street"></field>
            <field name="city"></field>
            <field name="zip"></field>
            <field name="phone"></field>
            <field name="email">info@my_company.com</field>
            <field name="website">www.my_company.com</field>
            <field name="image" type="base64" file="base/static/img/res_company_logo.png"/>
        </record>

        <record id="partner_my_company_us" model="res.partner">
            <field name="name">My company US</field>
            <field name="company_id" eval="None"/>
            <field name="customer" eval="False"/>
            <field name="is_company" eval="True"/>
            <field name="street"></field>
            <field name="city"></field>
            <field name="zip"></field>
            <field name="phone"></field>
            <field name="email">info@my_company.com</field>
            <field name="website">www.my_company.com</field>
            <field name="image" type="base64" file="base/static/img/res_company_logo.png"/>
        </record>

        <record id="company_my_company_hk" model="res.company">
            <field name="name">My company HK</field>
            <field name="partner_id" ref="partner_my_company_hk"/>
            <field name="currency_id" ref="base.USD"/>
        </record>

        <record id="partner_my_company_hk" model="res.partner">
            <field name="company_id" ref="company_my_company_hk"/>
        </record>

        <record id="company_my_company_us" model="res.company">
            <field name="name">My company US</field>
            <field name="partner_id" ref="partner_my_company_us"/>
            <field name="currency_id" ref="base.USD"/>
        </record>

        <record id="partner_my_company_us" model="res.partner">
            <field name="company_id" ref="company_my_company_us"/>
        </record>
    </data>
</odoo>

This is my original res_users.xml

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data noupdate="0">
        <record id="base.user_admin" model="res.users">
            <field name="groups_id" eval="[(4, ref('account.group_account_user'))]"/>
        </record>
    </data>
</odoo>

So I want to set those 2 newly created 2 companies into base.user_admin

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data noupdate="0">
        <record id="base.user_admin" model="res.users">
            <field name="groups_id" eval="[(4, ref('account.group_account_user'))]"/>

            <field name="company_id" ref="company_my_company_hk" />
            <field name="company_ids" eval="[(4, ref('company_my_company_hk')),(4, ref('company_my_company_us'))]" />
        </record>
    </data>
</odoo>

And it is not working. But when I uninstall the module and reinstall it works.

Why? I cannot uninstall the module just to make it work in the future. What are the limitations and how to bypass the limits?

Upvotes: 4

Views: 3470

Answers (4)

Gennadii
Gennadii

Reputation: 1

Maybe too late, but...

You can use odoo utils for upgrading modules, there is a function specifically for that there: https://www.odoo.com/documentation/17.0/developer/reference/upgrade_utils.html#update_record_from_xml

Upvotes: 0

Qaidjohar Barbhaya
Qaidjohar Barbhaya

Reputation: 56

More refined solution to @CZoellner 's solution is:

<function name="toggle_noupdate" model="ir.model.data" eval="['res.users', ref('base.user_admin')]"/>

Upvotes: 2

CZoellner
CZoellner

Reputation: 14768

You can change the noupdate value on XML, change the desired fields and should take the change on noupdate back.

<!-- Allow updating on noupdate=True records -->
<function name="write" model="ir.model.data">
    <function name="search" model="ir.model.data">
        <value
            eval="[('module', '=', 'base'), ('name', '=', 'user_admin')]" />
    </function>
    <value eval="{'noupdate': False}" />
</function>

<record id="base.user_admin" model="res.users">
    <!-- change fields here -->
</record>

<!-- Revoke noupdate change -->
<function name="write" model="ir.model.data">
    <function name="search" model="ir.model.data">
        <value
            eval="[('module', '=', 'base'), ('name', '=', 'user_admin')]" />
    </function>
    <value eval="{'noupdate': True}" />
</function>

Upvotes: 11

tom10271
tom10271

Reputation: 4649

External ID user_admin in base has been marked as noupdate.

And noupdate cannot be overridden.

Unless I run

UPDATE ir_model_data SET noupdate=False WHERE name = 'user_admin' and module='base';

Otherwise I won't be able to update it when I upgrade the module

Upvotes: 1

Related Questions