2016-12-16 189 views
0

我想從one2many字段創建記錄的同時爲多個字段設置默認值,默認值將取自父模型。從one2many字段創建記錄時設置默認值 - odoo

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') 

問題: 現在我想做的是,我想從 採購訂單設定的貨幣和公司,而現金預測記錄會從 PO窗體視圖創建,但我不知道該怎麼做。

注意:我不能採取貨幣或公司相關領域或 功能因爲有哪個公司和 貨幣應手動輸入一些其他情況,在沒有PO參考將被設置。

PO表單視圖

<page string="Deliveries &amp; 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> 

任何一個可以建議我,我應該在這種情況下怎麼辦?

回答

1

我得到了如何做到這一點。

爲了直接設置默認值,而在 one2many領域添加記錄,我們需要設置上下文值與前綴 default_field_name:值

上下文= 「{ 'default_currency_id':currency_id, 'default_company_id':COMPANY_ID}」

注意:當您創建與新記錄新紀錄, 如果您在one2many提供價值active_id不可用型號,然後 活動ID(S)不會在那裏。只有在保存 父記錄後方可訪問。

解決方案:

<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)]" />