从 Odoo 14 中的 Many2One 值列表中设置默认值(在子视图中自动选择一行)

问题描述 投票:0回答:2

我有一个父模型(植物),它在上下文中默认发送active_id(plant_id):

class Plant(models.Model):
    _name = 'db.plant'
    _description = 'db.plant'
    name = fields.Char("Name", required=True)
    ...

植物视图,它们之间具有many2one关系:

<odoo>
  <data>

    <record id="plant_list_view" model="ir.ui.view">
        <field name="name">db.plant.view</field>
        <field name="model">db.plant</field>
        <field name="arch" type="xml">
            <tree>
                <field name="name"/>
                ...
            </tree>
        </field>
    </record>

    <record id="plant_form_view" model="ir.ui.view">
        <field name="name">db.form.view</field>
        <field name="model">db.plant</field>
        <field name="arch" type="xml">
            <form>
                <sheet>
                    <group string="Plant">
                        <field name="name"/>
                        ...
                    </group>
                    <group string="Components">
                        <notebook>
                            <page string="Data Sources">
                                <field name="data_source_ids" context="{'default_plant_id': active_id}">
                                    <tree >
                                        <field name="name"/>
                                        ...
                                        <!-- <field name="plant_id"/> -->
                                    </tree>
                                </field>
                            </page>
                        </notebook>
                    </group>
                </sheet>
            </form>
        </field>
    </record>

    <record id="plant_action" model="ir.actions.act_window">
        <field name="name">plant</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">db.plant</field>
        <field name="view_mode">tree,form</field>
    </record>

</data>

和他的子模型(Data_source)

class DataSource(models.Model):
    _name = 'db.data_source'
    _description = 'db.data_source'

    name = fields.Char("Name", required=True)
    plant_id = fields.Many2one('db.plant', name="Plant", required=True)

数据源视图

<odoo>
  <data>
    <record id="data_source_list_view" model="ir.ui.view">
        <field name="name">db.data_source.view</field>
        <field name="model">db.data_source</field>
        <field name="arch" type="xml">
            <tree>
                <field name="name"/>
                ...
            </tree>
        </field>
    </record>

    <record id="data_source_form_view" model="ir.ui.view">
        <field name="name">db.form.view</field>
        <field name="model">db.data_source</field>
        <field name="arch" type="xml">
            <form>
                <group>
                    <field name="name"/>
                    ...
                </group>
                <group>
                    <field name="plant_id" domain="[('id', '=', context.default_plant_id)]" />
                </group>
                <group>
                </group>
            </form>
        </field>
    </record>

    <record id="data_source_action" model="ir.actions.act_window">
        <field name="name">Data Source</field>
        <field name="type">ir.actions.act_window</field>
        <field name="res_model">db.data_source</field>
        <field name="view_mode">tree,form</field>
    </record>
</data>

我知道孩子收到了 default_plant_id ,因为 plaint_id 域仅过滤我的 plant_id 但我无法将其自动设置为默认值。我需要单击列表并选择我的 plant_id(这是列表中的唯一元素)。

我还尝试通过两种方式在子项(data_source)中添加 @api.on_change("plant_id")

选项1

    @api.onchange('plant_id')
    def onchange_plant_id(self):
    if self.plant_id:
        context = dict(self._context or {})
        return [(0,0,{'plant_id': context.get('default_plant_id')})] 

给我这个例外

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/albertocrespo/tools/odoo/odoo/http.py", line 640, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
  File "/Users/albertocrespo/tools/odoo/odoo/http.py", line 316, in _handle_exception
    raise exception.with_traceback(None) from new_cause
AttributeError: 'list' object has no attribute 'get'

选项 2(我不确定要发送什么作为响应,但 write 语句也不起作用):

    @api.onchange('plant_id')
    def onchange_your_many_to_one_field(self):
      context = dict(self._context or {})
      self.plant_id.write({'id':context.get('default_plant_id')})})
      res = {}
      return res

在保存之前,我需要在数据源视图中自动选择我的植物作为选项。

odoo odoo-14 many2one
2个回答
0
投票

不需要 onchange 方法或 xml 端的任何内容 - 上下文级别。您可以简单地在关系 one2many 字段

plant_id
中使用字段
data_source_ids

试试这个:

data_source_ids = fields.One2many("db.data_source", "plant_id")

-1
投票

你解决了吗? 我需要在 Many2one 领域做同样的事情,但没有实现,你能帮助我吗...

© www.soinside.com 2019 - 2024. All rights reserved.