Guide To Odoo Default Fields

In this tutorial i will be explaining about the default field types that is supported by the Odoo. This will be based on the new api.

Odoo fields can be classified into two different categories basic fields and relational fields. Along with fields we can pass some keyword arguments to define the field much more. Some kwargs are default for every field others are unique to particular field. Now in Odoo all fields are defined as part of the class. Each field represent a column of that model.

// Basic definition of the field
from openerp import models, fields
class Model(models.Model):
_name = 'model_name'
name = fields.Char( string="Name", compute="_compute_function", store=True, index=True, readonly=True, inverse="_inverse_function" required=True, translate=True, help='help note', company_dependent=True, search='_search_function', default="any_value", groups='base.group_user', states="draft", copy=True )
// Simple definition of the field this will also work
name = fields.Char()

Information about the kwargs can be found below. There are also field specific kwargs that i will explain later in this tutorial.

// kwargs definition
string="Name" // represent the field name, not mandatory as Odoo will take the field name as Capitalized.
compute="_compute_function" // define the field as compute field
store=True // if the field is computed it will store the value
index=False // Tells Odoo to index for faster searches replaces the select kwarg
readonly=True // Make the field readonly in view
required=True // Making the field mandatory
translate=True // enable the translation on the field
help='help note' // Help note to display in field
company_dependent=True // Transform field to
search='_search_function' // Enables search on particular filed
default="any_value" // For setting the default value for a field
groups='base.group_user' // For setting the group for a field
states="draft" // For applying the conditional kwargs based on state
copy=True // Tell's Odoo whether the field value need to be duplicated

The basic fields in the Odoo are as follows

Char Fields

Like the name suggest this type of field is used to store character values. The syntax of the char field is given below

char_field = fields.Char() // simple definition
// Specific kwargs
size=100 // for defining the size of the data
translate=False // enabling the translation

Text Fields

This is same as the Char fields.But Text fields are used for storing multi line string values.

notes = fields.Text('Internal Notes') // simple definition
// Specific kwargs
translate=False // enabling the translation

Selection Fields

This is Odoo equivalent of static drop down menu. Selection values consist of key value pair of tuples. Value can be either number or string. Selection fields can be used in workflow creation. selection field value can be defined dynamically using a function.

state = fields.Selection([('draft', 'Not Available')], string="State") // simple definition
state = fields.Selection(selection='_select_function', string="State") // defining the values using a function
state = fields.Selection(selection_add=[('available', 'Available')]) // adding the new value to the selection field
// Specific kwargs
selection=[('available', 'Available')] // a list of tuple or a callable name that take recordset as input
size= 1 // is mandatory when using indexes that are integers, not strings

HTML Fields

This is same as the char field.but the string is stored in rich text in the html format.

description = fields.Html('Description') // simple definition
// Specific kwargs
sanitize=True // sanitize tag is for removing insecure tags
strip_style=False // strip_style is for removing the style elements.
translate=False // for translating the value

Binary Fields

This is used for storing the images or documents.

cover = fields.Binary('Cover') // simple definition

Boolean Fields

stores true/false value in it

true = fields.Boolean('is it true ?') // simple definition

Date Fields

This is for storing the date.Odoo handles the date as a string but it is stored as date object in database.

date = fields.Date() // simple definition

DateTime Fields

This is for storing the datetime value. This is in UTC format.

date = fields.Datetime() // simple definition

Helper Functions

There are some helper function provided by the date and datetime fields. Which can be used as below.

from openerp import fields
Date Helper Functions // returns current system date string
fields.Date.context_today(record) // returns the current day according to the timezone of the record or recordset
fields.Date.from_string(string_value) // parses the date string into date object
fields.Date.to_string(date_value) // represent the date object as a string value
Datetime Helper Functions // returns current system datetime as string suitable for default values
fields.Datetime.context_timestamp(record,value) // converts a value into a timezone aware date. not suitable for default values
fields.Datetime.from_string(string_value) // parses the datetime string into date object
fields.Datetime.to_string(date_value) // represent the datetime object as a string value

Monetary Fields

This is for storing the amount in a specific currency. The precision of the monetary fields are taken from the res.currency model,usually there will be a second field to specify which currency to be used.

price = fields.Monetary('Price' ) // simple definition
// Specific kwargs
currency_id = fields.Many2one('res.currency', "Currency") // for using in currency field
currency_field=currency_id // This is an optional parameter

Compute Fields

In the new api compute fields replaces the fields.function. It uses compute kwarg for converting any normal fields to compute fields. The compute keyword argument holds the function to executed for calculating the value of the field. By default these are non editable and non searchable and also non writable. But you can use the specific kwargs defined below for overruling that. Computed values are calculated at runtime and stored in the cache so that it is not needed to recalculate it again. we can use special decorator @api.depends for specifying the fields related to the compute field. It is always wise to add a default value for the compute fields.

count = fields.Float(string="Count", compute='_compute_count') // simple definition
// Specific kwargs
inverse='_inverse_count' // this is for changing the fields that are related to the computed field also making the field editable
search='_search_count' // enabling the search in compute field
store=False // for storing the value of computed fields
compute_sudo=False // compute the fields as super user

There is also a multi parameter in the field function definition it's related to use the same method to return the value of many function fields. It's only used in the old api and not needed in the new api. It's a matter of performance to call the function only one time for all the function fields with the same multi value and when used the function return value it's different to be able to return all the values of the fields using a dict for every id in the list.

Related fields in the odoo is noted by the related kwarg. We can convert any field in to related fields. It will calculate the field value based on a Many2one field. The related kwarg replaces the fields.related field in the old api.

user_city = fields.Char('User City', related='') // simple definition
// Specific kwargs
related_sudo=False // compute the fields as super user
store=True // storing the value

Reference Fields

Store an arbitrary reference to a model and a row

ref_id = fields.Reference(selection='_referencable_models') // simple definition
// Specific kwargs
selection='_referencable_models' // returns a list of tuple or a callable name that take record set as input

Relational fields

Odoo has three major relational fields in it which are following

Many2one Fields

This represents a many to one relation to a model which is same as drop down in html and which can have optional parameters such as ondelete,context,domain etc. in the db level it will store the id of the referencing model and also this will create a db constraint as we mentioned below.By default no db index is created but this can be done using the index =True.

user_id = fields.Many2one('res.users',string="User", // simple definition
// Specific kwargs
comodel_name='res.users' // name of the opposite model
ondelete = 'set null' // on delete constraints for that field
domain=[] // setting up domain for the Many2one
context={} // passing the context
delegate=True // for accessing the fields of the targeted models in the current model same as _inherits

One2many Fields

One2many as the name refers, it is a relation to a model,just like a tree and in that we have to specify the model and the fields that we want to display in the one2many.These have no actual representation in the db. Adding a One2many to a model we need to have a Many2one relation in the targeted model and have to specify the many2one field in the one2many field. These are non as comodel_name and inverse_name in the Odoo level but the name is not necessary the position is enough.

line_ids = fields.One2many(comodel_name='res.users', inverse_name='line_id') // simple definition
// Specific kwargs
comodel_name='res.users' // name of the opposite model
inverse_name='line_id' // related field in the opposite model
limit=100 // set an optional limit on the set of records

Many2many Fields

This for adding multiple records of another model to the current model. It will not add any new columns to the db instead of that it will create a new table with the ids of the two table like they are linked to each other.The table name is created by Odoo automatically by combining the two table name and adding the suffix _rel this can be overwrite by the relation kwarg as it is more secure, if the corresponding table names are large then the resulting table name will exceed the Postgres limit of 63 character. We can use the predefined widgets in Odoo such as many2many_tags, many2many etc. to alter the way it is displayed.

user_ids = fields.Many2many('res.users', string='Users') // simple definition
// Specific kwargs
comodel_name='res.users' // name of the opposite model
relation='table_name' // name of the table
column1='col_name' // this is the field of the many2one field of the relational table which is linking to this model
column2='col_name' // this is the many2one field in the relational table which is linking to comodel

These are the default fields in Odoo. But we can create and customize any new fields as Odoo is open source and it's code structure is very simple to understand.