Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
2 Replies
10506 Tampilan

I have read https://www.odoo.com/forum/help-1/question/how-to-extend-context-value-110103 which explains how one can extend a context value in python. Can context also be extended in view definitions in xml ?

For example there's a field "partner_id" in view "invoice_form" (odoo/addons/account/views/account_invoice_view.xml) that has a certain context set already. I'd like to extend this context.

I understand I can create and form that inherits from this one and redefine the field. However I only want to add something to the context rather than replace the context. Is that possible ?

I'm currently using odoo v11

Avatar
Buang

Hope you are looking this: t.ly/y6AXG

Penulis

@Sehrish Thank you for your comment. However it only explains how to add context to a form field, not how to extend an existing context.

To continue with the example from your link: imagine I'm extending your view in my own module. I can't directly edit yours because it's in a different module which I don't own. And suppose I want to set a default value for the sequence field. To keep it simple, let's just say I want the default to be 15 in case my extended view is used.

For that I can write a new View record with the same name as your base form and alter the value of the context attribute on the many2one field:

<record model="ir.ui.view" id="external_evaluator_form_override">

<field name="name">my_module.external_evaluator.form</field>

<field name="inherit_id" ref="your_module.external_evaluator_form"/>

<field name="model">external.evaluator</field>

<field name="arch" type="xml">

<field name="external_evaluation_ids" position="attributes">

<attribute name="context">{'default_state':state, 'default_sequence': 15}</attribute>

</field>

</field>

</record>

Note I have in this case set both a default_state and a default_sequence. I'm effectively replacing the existing context on your field. Instead however I'd like to only add a default_sequence and leave the other elements in the context untouched. The reason is that a future version of the parent form may have a different context which I would like to inherit as well without having to redo changes in my model.

Just imagine in a future version of the parent external_evaluator_form the context would be set to

{'default_state': state, 'default_institute': 'something'}

With my context replacement, I wouldn't get the default_institute value in my extended view. So is there a way to achieve this anyway - that is, a way to only specify the specific part of the context I want to modify, but otherwise inherit the default context as is without explicitly copying it ?