You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo v8弹窗内Tree视图默认值设置技术咨询

Setting Default Values for Embedded Tree View in Odoo v8 Popup

Got it, let's figure out how to set default values for the embedded Tree view in your Odoo v8 popup. Since you already have the popup form up and running with a Tree inside, there are a couple of targeted ways to make this work, depending on whether you want defaults tied to the parent form or set specifically for this popup:

1. Attach Defaults Directly to the One2many/Many2many Field in the Popup Form

Most likely, your embedded Tree is linked to the parent mcu.record via a one2many or many2many field (let's assume it's named line_ids for this example). You can add a context attribute to this field in your popup form view to pass default values directly to new records in the Tree.

Here's how to update your existing form view code:

<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>
        <!-- Add context to your embedded Tree field -->
        <field name="line_ids" 
               context="{'default_mcu_record_id': active_id, 
                         'default_parent_mcu_data_id': parent_mcu_data_id,
                         'default_status': 'draft'}"/>
      </sheet>
    </form>
  </field>
</record>
  • active_id refers to the ID of the current mcu.record record loaded in the popup, perfect for linking child Tree records to their parent.
  • parent_mcu_data_id pulls the value directly from the parent form's field (or you can hardcode 479 like your existing context if needed).
  • Any default_<field_name> entry here will automatically apply to new records created in the embedded Tree.

2. Pass Defaults via the do_action Call

If you're triggering the popup using the do_action method in Python, you can extend the context there to pass defaults that the Tree view can inherit.

First, update your do_action code to include Tree-specific defaults:

return {
    'type': 'ir.actions.act_window',
    'name': 'MCU Record Popup',
    'res_model': 'mcu.record',
    'res_id': your_record_id,  # Replace with your target record ID
    'view_type': 'form',
    'view_mode': 'form',
    'views': [(self.env.ref('your_module.view_mcu_record_popup_form').id, 'form')],
    'target': 'new',
    'context': {
        'default_parent_mcu_data_id': 479,
        # Add defaults for the Tree view's model here
        'default_tree_field_1': 'fixed_value',
        'default_tree_field_2': self.env.user.id,
    },
}

Then, make sure your Tree view (for the child model, e.g., mcu.record.line) is set up to read these context values. Here's an example Tree view definition:

<record id="view_mcu_record_line_tree" model="ir.ui.view">
  <field name="name">mcu.record.line.tree</field>
  <field name="model">mcu.record.line</field>
  <field name="context">
    {'default_field_1': context.get('default_tree_field_1'), 
     'default_field_2': context.get('default_tree_field_2')}
  </field>
  <field name="arch" type="xml">
    <tree>
      <field name="field_1"/>
      <field name="field_2"/>
      <!-- Your other Tree fields -->
    </tree>
  </field>
</record>

This way, when the popup loads, any new records added to the Tree will automatically pick up these defaults from the context passed via do_action.

3. Quick Note on Global Defaults

If you want a default value to apply everywhere the Tree view is used (not just this popup), you can override the default_get method in the Tree's model. For example:

class McuRecordLine(models.Model):
    _name = 'mcu.record.line'
    
    def default_get(self, fields):
        res = super(McuRecordLine, self).default_get(fields)
        res['parent_mcu_data_id'] = 479
        return res

But keep in mind this will affect all instances of this model's Tree views, so use this only if you want the default to be universal.

内容的提问来源于stack exchange,提问作者strike_noir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:27:24