Odoo’s Coding Tips

Proper way to get Today’s Date and Now’s Datetime

  • Now’s Datetime > fields.Datetime.now()
  • Today’s Date > fields.Date.context_today(self)

Above command resolve Timezone problem, unlike using python alone, i.e., time.strftime(‘%Y-%m-%d’)

Change how the many2one dropdown is displayed through the use of name_get()


On view

<field name=”calendar_period_id” context=”{‘use_calendar_name’: True}” />

On class

class AccountPeriod(models.Model):
    _inherit = 'account.period'

    calendar_name = fields.Char(
        string='Calendar Name',

    def name_get(self):
        res = super(AccountPeriod, self).name_get()
        if self._context.get('use_calendar_name', False):
            res = []
            for rec in self:
                res.append((rec.id, rec.calendar_name))
        return res

Sorting lines with different field on Tree view

<record id="_view_sale_order_line_tree" model="ir.ui.view">
    <field name="name">sale.order.line.tree1</field>
    <field name="model">sale.order.line</field>
    <field name="type">tree</field>
    <field name="priority" eval="1"/>
    <field name="arch" type="xml">
        <tree string="Sales Order Lines" default_order="name">
            <field name="name"/>
            <field name="sequence"/>
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.