Search code examples
odooodoo-8

How to set default field value from xml code in odoo?


In sales order module I have created a new customised form view, in sale.py file I have created a new field 'is_sample'

'is_sample': fields.boolean("Specimen Order", store=False),

I want to set its default value from xml code so that it doesn't get affected in default form view. I have tried in four ways,

1)<field name="is_sample" eval="True"/>
2)<field name="is_sample" domain="[('is_sample','=',True)]"/>
3)<record id="action_specimen_orders" model="ir.actions.act_window">
        <field name="type">ir.actions.act_window</field>
        <field name="context">{'is_sample': 'True'}</field>
       ...
4)<record id="action_specimen_orders" model="ir.actions.act_window">
        <field name="type">ir.actions.act_window</field>
        <field name="domain">[('is_sample','=','True')]</field>
       ...

Solution

  • There is an easiest way to set default value from the xml, generally you can it for all those fields of the model. For that you need to add dictionary key/value pair to set default value for any field.

    General syntax

    default_field_name : default_value 
    

    To set default value you need to pass context with window action in which you need to set one key/value pair in context as shown in syntax. default_ is the prefix which needs to be set with field name as key.

    Try following:

    <record id="action_specimen_orders" model="ir.actions.act_window">
            <field name="type">ir.actions.act_window</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="context">{'default_is_sample': True}</field>
    </record>