从one2many字段创建记录时设置默认值-odoo [英] Set default value while creating record from one2many field - odoo

查看:679
本文介绍了从one2many字段创建记录时设置默认值-odoo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个一个很多字段中创建记录时为多个字段设置默认值,因为该默认值将从父模型中获取.

I want to set default value for multiple fields while creating records from one2many field, in that default value will be taken from the parent model.

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表单视图,但我不知道该怎么做.

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 &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>

任何人都可以建议我,在这种情况下我该怎么办?

Can any one suggest me, what should I do in that case ?

推荐答案

我已经知道了怎么做.

为了在添加记录时直接设置默认值 one2many 字段,我们需要在带前缀的上下文中设置值 default_field_name:值.

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}"

注意:在创建新记录时,active_id不可用,并且 如果您在one2many模型中提供了价值,则使用该新记录 有效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模型的字段中添加域,并且要使用父模型的值,则可以通过以下方式进行操作.

If you want to add domain in fields of one2many model and in that you want to use the values of parent model then you can do it by the following way.

<field name="company_id" domain="[('id','=',parent.company_id)]" />

这篇关于从one2many字段创建记录时设置默认值-odoo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆