从 one2many 字段创建记录时设置默认值 - odoo
Set default value while creating record from one2many field - odoo
我想在从 one2many 字段创建记录时为多个字段设置默认值,因为默认值将从父模型中获取。
Odoo 模型结构
class purchase_order(models.Model):
_inherit='purchase.order'
cash_forecast_ids = fields.One2many(comodel_name='cash.forecast', inverse_name='purchase_order_id', string='Payment Schedules')
class cash_forecast(models.Model):
_name='cash.forecast'
purchase_order_id = fields.Many2one(comodel_name='purchase.order', string='PO', select=True, copy=False)
foreign_currency_amount = fields.Float("Foreign Currency Amount", copy=False)
currency_id = fields.Many2one(comodel_name="res.currency", string="Currency", copy=False)
company_id = fields.Many2one(comodel_name='res.company', string='Company')
Problem :
Now what I want to do is, I want to set currency and company from the
purchase order while the cash forecast record will be created from the
PO form view, but I don't know how to do it.
NOTE : I am not able take currency or company field related or
functional because there are few other situations for which company and
currency should be entered manually and in that no PO reference will be set.
PO 表单视图
<page string="Deliveries & Invoices" position="after">
<page string="Payment Scedule">
<field name="cash_forecast_ids" attrs="{'readonly' : [('state','in',['done','cancel'])]}">
<tree string="Payment Scedule" editable="bottom">
<field name="name"/>
<field name="cash_forecast_type_id" required="1" domain="[('add_to_po_payment_schedule','=',True)]" />
<field name="note" />
<field name="forecast_date" />
<field name="period_id" required="1" />
<field name="foreign_currency_amount" required="1" />
<field name="currency_id" required="1" />
<field name="purchase_order_id" invisible="1"/>
<field name="company_id" required="1" />
</tree>
</field>
</page>
</page>
谁能建议我,在那种情况下我该怎么办?
我知道怎么做了。
In order to set default values directly while adding records in
one2many fields, we need to set values in context with prefix
default_field_name : value.
context="{'default_currency_id' : currency_id, 'default_company_id' : company_id}"
Note: active_id is not available while you creating new record and
with that new record if you provide value in one2many model then
active id(s) won't be there. It's only accessible once you save the
parent record.
解法:
<field name="cash_forecast_ids" context="{'default_currency_id' : currency_id, 'default_company_id' : company_id}">
<tree string="Payment Scedule" editable="bottom">
<field name="name"/>
<field name="forecast_date" />
<field name="foreign_currency_amount" required="1" />
<field name="currency_id" domain="[('id','=',parent.currency_id)]" required="1" />
<field name="purchase_order_id" invisible="1"/>
<field name="company_id" domain="[('id','=',parent.company_id)]" required="1" />
</tree>
</field>
如果你想在one2many模型的字段中添加域,并且你想使用父模型的值,那么你可以通过以下方式来完成。
<field name="company_id" domain="[('id','=',parent.company_id)]" />
要将父值发送到 one2many 字段,请在 XML 中的 one2many 字段上定义上下文,以使用上下文附加 "default_" 作为所需字段的前缀,例如"default_state" 作为键,值使用父 table 字段名称。
<field name="external_evaluation_ids" context="{'default_state':state}">
<tree>
<field name="state"/> <!--newly created field in one2many table-->
<field name="child_table_field1"/>
<field name="child_table_field2"/>
<field name="child_table_field2"/>
</tree>
</field>
在上面的代码片段中,我们有一个 one2many 字段,我们在该字段中定义了具有键值的上下文。这里键的名称将从 "default_" 开始,而不是字段名称,值应该是我们要在 one2many 弹出表单视图中显示的父 table 字段名称。
欲了解更多信息 http://learnopenerp.blogspot.com/2018/01/get-parent-form-value-in-one2many-form.html
我想在从 one2many 字段创建记录时为多个字段设置默认值,因为默认值将从父模型中获取。
Odoo 模型结构
class purchase_order(models.Model):
_inherit='purchase.order'
cash_forecast_ids = fields.One2many(comodel_name='cash.forecast', inverse_name='purchase_order_id', string='Payment Schedules')
class cash_forecast(models.Model):
_name='cash.forecast'
purchase_order_id = fields.Many2one(comodel_name='purchase.order', string='PO', select=True, copy=False)
foreign_currency_amount = fields.Float("Foreign Currency Amount", copy=False)
currency_id = fields.Many2one(comodel_name="res.currency", string="Currency", copy=False)
company_id = fields.Many2one(comodel_name='res.company', string='Company')
Problem : Now what I want to do is, I want to set currency and company from the purchase order while the cash forecast record will be created from the PO form view, but I don't know how to do it.
NOTE : I am not able take currency or company field related or functional because there are few other situations for which company and currency should be entered manually and in that no PO reference will be set.
PO 表单视图
<page string="Deliveries & Invoices" position="after">
<page string="Payment Scedule">
<field name="cash_forecast_ids" attrs="{'readonly' : [('state','in',['done','cancel'])]}">
<tree string="Payment Scedule" editable="bottom">
<field name="name"/>
<field name="cash_forecast_type_id" required="1" domain="[('add_to_po_payment_schedule','=',True)]" />
<field name="note" />
<field name="forecast_date" />
<field name="period_id" required="1" />
<field name="foreign_currency_amount" required="1" />
<field name="currency_id" required="1" />
<field name="purchase_order_id" invisible="1"/>
<field name="company_id" required="1" />
</tree>
</field>
</page>
</page>
谁能建议我,在那种情况下我该怎么办?
我知道怎么做了。
In order to set default values directly while adding records in one2many fields, we need to set values in context with prefix default_field_name : value.
context="{'default_currency_id' : currency_id, 'default_company_id' : company_id}"
Note: active_id is not available while you creating new record and with that new record if you provide value in one2many model then active id(s) won't be there. It's only accessible once you save the parent record.
解法:
<field name="cash_forecast_ids" context="{'default_currency_id' : currency_id, 'default_company_id' : company_id}">
<tree string="Payment Scedule" editable="bottom">
<field name="name"/>
<field name="forecast_date" />
<field name="foreign_currency_amount" required="1" />
<field name="currency_id" domain="[('id','=',parent.currency_id)]" required="1" />
<field name="purchase_order_id" invisible="1"/>
<field name="company_id" domain="[('id','=',parent.company_id)]" required="1" />
</tree>
</field>
如果你想在one2many模型的字段中添加域,并且你想使用父模型的值,那么你可以通过以下方式来完成。
<field name="company_id" domain="[('id','=',parent.company_id)]" />
要将父值发送到 one2many 字段,请在 XML 中的 one2many 字段上定义上下文,以使用上下文附加 "default_" 作为所需字段的前缀,例如"default_state" 作为键,值使用父 table 字段名称。
<field name="external_evaluation_ids" context="{'default_state':state}">
<tree>
<field name="state"/> <!--newly created field in one2many table-->
<field name="child_table_field1"/>
<field name="child_table_field2"/>
<field name="child_table_field2"/>
</tree>
</field>
在上面的代码片段中,我们有一个 one2many 字段,我们在该字段中定义了具有键值的上下文。这里键的名称将从 "default_" 开始,而不是字段名称,值应该是我们要在 one2many 弹出表单视图中显示的父 table 字段名称。 欲了解更多信息 http://learnopenerp.blogspot.com/2018/01/get-parent-form-value-in-one2many-form.html