how to set default value as a years in a field odoo 11?

564 Views Asked by At
fiscal_id = fields.Many2one('account.fiscalyear', 'Financial Year',default= '2020-2021')

I want default value as 2020-2021 in many2one field but i don't know how to assign the default value??

2

There are 2 best solutions below

1
kerbrose On

you could use default lambda funtion as following:

fiscal_id = fields.Many2one('account.fiscalyear', 'Financial Year', default=lambda self: self.env.ref('my_module.xml_id_related_to_the_record'))

in this case you need an xml id which will create the data record from xml file as following:

<odoo>
  <data>


    <record id="xml_id_related_to_the_record" model="account.fiscalyear">
      <field name="name">2020-2021</field>
    </record>


  </data>
</odoo>

please note that you may need to map the xml data correctly according to your model definition.

don't hesitate to let us know if it is working

0
Kenly On

You can use a function to search for the default record using the current date and return the corresponding record.

In the following example we return the first record found and set it as a default value for fiscal_id field.

@api.model
def _get_default_fiscalyear(self):
    return self.env['account.fiscalyear'].search([], limit=1)

fiscal_id = fields.Many2one('account.fiscalyear', 'Financial Year',default= _get_default_fiscalyear)