0

I am using Odoo v8. Currently I created a pop up (do_action function) with a tree inside.

This is the view.

    <record id="view_mcu_record_popup_form" model="ir.ui.view">
        <field name="name">mcu.record.popup.form</field>
        <field name="model">mcu.record</field>
        <field name="context">{'default_parent_mcu_data_id': 479}</field>
        <field name="arch" type="xml">
            <form>
                <sheet>
                    <group>
                        <field name="name" readonly="1" />
                    </group>
                    <field name="mcu_data_ids" domain="[('parent_mcu_data_id', '=', 479)]" context="[('default_parent_mcu_data_id', '=', 479)]" widget="one2many_list">
                        <tree editable="bottom" create="true" delete="true">
                            <field name="name" />
                            <field name="val_alphabet" />
                            <field name="val_float" />
                            <field name="val_yesno" />
                            <field name="ref_range" />
                            <field name="unit" />
                            <field name="highlight" />
                            <field name="note" />
                        </tree>
                    </field>
                </sheet>
                <footer>
                    <button name="save_data" string="Save" type="object" class="oe_highlight" />
                    or
                    <button string="Cancel" class="oe_link" special="cancel" />
                </footer>
            </form>
        </field>
    </record>

I wanted to give default value to a field as you can see I put

<field name="context">{'default_parent_mcu_data_id': 479}</field>

<field name="mcu_data_ids" domain="[('parent_mcu_data_id', '=', 479)]" context="[('default_parent_mcu_data_id', '=', 479)]" widget="one2many_list">

These two lines does nothing. The new row parent_mcu_data_id remains empty.

How to give them a default value from view (not from the model please).

Here I put the variable that I give to do_action function

        var new_tree_action = {
            type: "ir.actions.act_window",
            name: act_title,
            res_model: "mcu.record",
            res_id: this.field_manager.datarecord.id,
            view_mode: "form",
            view_type: "form",
            views: [[false, "form"]],
            target: "new",
            context: {
                "default_patient_mcu_id": this.field_manager.datarecord.id,
                "default_parent_mcu_data_id": 479,
                "context_id": context_id,
                "context_parent_id": context_parent_id,
                "variety": context_variety,
                "ref_range": context_ref_range,
                "note": context_note,
                "highlight": context_highlight,
                "unit": context_unit,
                "res_id": context_res_id,
            },
        }

I fill view id [[false, "form"]] later using ir_model_data.call("get_object_reference", ["medical_checkup", view_id]).then(function(results) {

To give more information I decided to give a hint of the model

class McuRecordInherit(models.Model):
    _auto = True
    _inherit = "mcu.record"
    _description ="Medical Checkup Record"

    mcu_data_ids = fields.One2many("mcu.data", "patient_mcu_id", string="Medical Checkup Data")
strike_noir
  • 4,080
  • 11
  • 57
  • 100

2 Answers2

0

If parent_mcu_data_id field is declared in mcu.record model, @CZoellner's answer is the correct one. But if it is in the model pointed by the relational field mcu_data_ids, do the following:

Add the field parent_mcu_data_id to the tree view, otherwise domain and context with default_ keys won't work:

<field name="mcu_data_ids" domain="[('parent_mcu_data_id', '=', 479)]" context="[('default_parent_mcu_data_id', '=', 479)]" widget="one2many_list">
    <tree editable="bottom" create="true" delete="true">
        <field name="name" />
        <field name="val_alphabet" />
        <field name="val_float" />
        <field name="val_yesno" />
        <field name="ref_range" />
        <field name="unit" />
        <field name="highlight" />
        <field name="note" />
        <field name="parent_mcu_data_id" />
    </tree>
</field>

And remove the field context you wrote above (the one which is child of record tag). It does nothing in a ir.ui.view.

forvas
  • 9,801
  • 7
  • 62
  • 158
  • I have tested this, and re-tested again after you answered. It doesn't work. Maybe it worked in v9++. Thanks by the way. – strike_noir Apr 21 '18 at 09:48
0

You have to update the context on the action not in the view. At the time the view is rendered, the default values are already evaluated.

If you open up a form view from a button of another view, update its context:

<button name="do_action" string="Action"
    context="{'default_field_another_model': 'default'}" />
CZoellner
  • 13,553
  • 3
  • 25
  • 38