Skip to Content
Menu
This question has been flagged
1 Reply
5492 Views

I have a model that transitions through several states throughout its lifecycle. As the model transitions between states, field attributes change. For example, when in a 'draft' state, some fields of the model are invisible. When in an 'open' state, some fields are readonly.

Right now, all of this view logic is contained within a single view. Consequently, the xml is difficult to read and it is easy to make mistakes in setting field attributes. It seems a better approach would be to have a separate view defined for each model state; however, to do this I would need to know how to select a view based off of a model's state.

How might I implement the conditional selection of views based off of a model's state?

Avatar
Discard
Best Answer

you can use odoo server actions. for eg:

<record model="ir.actions.server" id="print_instance">
    <field name="name">Res Partner Server Action</field>
    <field name="model_id" ref="model_res_partner"/>
    <field name="code">
        if object.some_condition():
            action = {
                "type": "ir.actions.act_window",
                "view_mode": "form",
                "res_model": object._name,
                "res_id": object.id,
            }
    </field>
</record>

so according to the state you just call the action with particular view id. To know more about calling an action with perticular view id refer this link.

https://www.odoo.com/documentation/11.0/reference/actions.html#server-actions-ir-actions-server

Avatar
Discard
Author

Thanks for your response.

There is still a great deal that I don't understand regarding actions in Odoo. The documentation covers their API, but its difficult to understand how and when to use them. For example, the documentation you linked describes a server action as allowing "...triggering complex server code from any valid action location." But I don't know what a "valid action location" is.

Related Posts Replies Views Activity
4
Jan 24
20581
2
Jan 24
771
0
Aug 23
951
1
Apr 22
8064
2
Oct 21
7033