如何在Odoo中的某个表单视图中设置默认字段值?

时间:2017-12-01 03:42:09

标签: field default-value odoo-9 odoo odoo-view

如何使用xml在此表单中设置Job_id中的默认值,而不需要在python文件中设置?

<record id="form_student_stu" model="ir.ui.view">
    <field name="name">Student Info</field>
    <field name="model">youth_and_scholarship.youth_and_scholarship</field>
    <field name="arch" type="xml">
        <form string="Job Form" version="9.0">
            <div class="widget_box">
                <div class="widget_title"><h5>Student Information</h5></div>
                <div>
                    <group col="1">
                        <field name="image" widget="image" class="oe_avatar oe_left" nolabel="1"/>
                        <field name="Job_id" invisible="1"/>
                    </group>
                    <group>
                        <field name="Kname"  placeholder="First Name and Last Name..." string="Name in Khmer" style="width:40%%"/>
                        <field name="Ename" placeholder="First Name and last Name..." string="Name in Latin" style="width:40%%"/>
                        <field name="Gender" string="Gender" style="width:40%%"/>
                        <field name="DOB" string="Date of birth"/>
                        <field name="Job_work" string="Accupation" style="width:40%%"/>
                    </group>
                </div>
            </div>
        </form>
    </field>
</record>

1 个答案:

答案 0 :(得分:1)

您可以使用一些指定XML ID(外部ID)的上下文。您可以将默认值添加到调用视图的操作,如下所示:

<record id="youth_and_scholarship_youth_and_scholarship_action" model="ir.actions.act_window">
    <field name="name">Student Info</field>
    <field name="type">ir.actions.act_window</field>
    <field name="res_model">youth_and_scholarship.youth_and_scholarship</field>
    <field name="view_type">form</field>
    <field name="view_mode">tree,form</field>
    <field name="context">{'default_job_id': ref('xml_id')}</field>
</record>

如果您打开一个字段中的视图,例如从many2one中打开视图,则可以将该上下文添加到该字段:

<field name="field_name" context="{'default_job_id': ref('xml_id')}" />

如果没有设置记录的XML ID,您可以通过Python代码获取ID并返回带有上下文的操作窗口:

job_id = self.get_job_id()
return {
    'name': 'Student Info',
    'type': 'ir.actions.act_window',
    'view_type': 'form',
    'view_mode': 'form',
    'res_model': 'youth_and_scholarship.youth_and_scholarship',
    'context': {'default_job_id': job_id }
}

备注

  • 密钥必须始终为:default_ + field_namedatabase_id

  • 您可以在此处查看所有XML ID:设置&gt;技术&gt;序列&amp;标识符&gt;外部标识符&gt;完整ID