How to index a field in Odoo

It is a good practice to create an index for the most searchable fields in the model. In this way, you can speed the database search performance. However, you should be careful not to add indexes on too many fields because the size of the database can rapidly increase. What means, you should carefully detect the most searchable fields and to create indexes for them only. If you are not familiar with database indexes, you can find more info here.

In Odoo, you can add an index on a particular field by setting up the index attribute on True in the field definition.

For example, if you want to add an index on the field work_hours in the PartnerContact model, you need to define it in this way:

from openerp import models

class PartnerContact(models.Model):
    _name = ''

    work_hours = fields.Float(string='Work Hours', index=True)


Leave a Reply

Your email address will not be published.