from odoo import models, fields
from odoo import models, fields
Example:
class TestModel(models.Model):
_name = 'test.model'
name = fields.Char(string="Name", required=True)
dob = fields.Date(string="DOB", required=True, help="Date of Birth")
Field Types:
Boolean: Store boolean values True and False
Example:
bool = fields.Boolean()
active = fields.Boolean(string="Active", default=True)
Char: Store string or characters with length.
Parameter:
size: Data will be trimmed into the specified size.
Example:
name = fields.Char()
name = fields.Char(string="Name", required=True)
proxy_ip = fields.Char(string='IP Address', size=45)
Text: Used to store long texts.
Example:
text = fields.Text()
notes = fields.Text(string='Terms and Conditions')
Integer: Fields to store integer values. Null value is not allowed. If there is no value then it returns zero.
Syntax:
num = fields.Integer()
Float: Used to store float values. Null value is not supported. If there is no value, it returns 0.0.
Parameter:
digits: it defines the precision and scale of the number. Scale is the number of digits after the decimal point and precision is the total number of significant digits in the number.
Syntax:
float_num = fields.Float()
amount = fields.Float(string="Amount", digits=(6, 2))
Date: Date field stores date. The field has some helpers:
> context_today: Returns current day date string based on timezone
> today: Returns current system date string
> from_string: Returns datetime.date() from string
> to_string: Returns date string from datetime.date
Example:
today = fields.Date.today()
fields.Date.context_today(self)
fields.Date.context_today(self,timestamp=datetime.datetime.now())
fields.Date.from_string(fields.Date.today())
fields.Date.to_string(datetime.datetime.today())
return:
‘2019-09-02’
‘2019-09-02’
‘2019-09-02’
datetime.datetime(2019, 9, 2, 5, 12, 18)
‘2019-09-02’
DateTime: It stores date and time in the same field. The field has some helpers.
> context_timestamp: Returns current day date string based on timezone
> now: Returns current system date string
> From_string: Returns datetime.date() from string
> to_string: Returns date string from datetime.date
Example:
fields.Datetime.context_timestamp(self,timestamp=datetime.datetime.now())
fields.Datetime.now()
fields.Datetime.from_string(fields.Datetime.now())
fields.Datetime.to_string(datetime.datetime.now())
Returns:
datetime.datetime(2019, 8, 1, 11, 22, 15, 5729, tzinfo= < DstTzInf 'Europe/Brussels' CEST + 2: 00:00 DST >)
'2014-06-15 19:26:13'
datetime.datetime(2014, 6, 15, 19, 32, 17)
'2014-06-15 19:26:13'
Binary: field stores the encoded file in base64 in column bytea.
file = fields.Binary()
Selection: Field stores text but the purpose is a selection widget.
selection: a list of tuple or a callable name that take recordset as input
size: the option size=1 is mandatory when using indexes that are integers.
Example:
select = fields.Selection([('a', 'A')])
select = fields.Selection(selection=[('a', 'A')])
select = fields.Selection(selection='_function_name')
Selection_add: when you are trying to extend a model if you want to add more possible values to an existing selection field you may use the selection_add keyword argument:
class TestModel(models.Model):
_inherit = 'test.model'
type = fields.Selection(selection_add=[('b', 'B'), ('c', 'C')])
Reference: Store an arbitrary reference to a model and a row.
selection: a list of tuple or a callable name that take recordset as input.
refer = fields.Reference([('model.name', 'String_string')])
refer = fields.Reference(selection=[('model.name', 'String_string')])
Html: Stores HTML and used as HTML Widget.
Example:
html_wid = fields.Html()
Many2one: Associates this object to a parent object via this field.
comodel_name: name of the related model
Delegate: if it is True, then it will make fields of the target model accessible from the current model.
Example:
user = fields.Many2one('res.users')
user = fields.Many2one(comodel_name='res.users')
user = fields.Many2one(comodel_name='res.partner', delegate=True)
One2many: Stores a relation against many rows of co-model:
comodel_name: name of the related model.
inverse_name: the relational column of the related model.
mod_ids = fields.One2many('res.users', 'rel_id')
mod_ids = fields.One2many(comodel_name='res.users', inverse_name='rel_id')
Many2many: Field stores multiple rows of a relation.
comodel_name: name of the related model.
relation: name of the relational table.
columns1: left column name of the relational table.
columns2: right column name of the relational table.
mod_ids = fields.Many2many('res.partner’)
mod_ids = fields.Many2many(comodel_name='res.partner',
relation='table_name',
column1='col_name',
column2='other_col_name')
The common parameters:
help: Provide a description of how the field should be used.
ondelete: To handle deletions in a related record.
Values are: 'restrict', 'no action', 'cascade', 'set null', and 'set default'.
readonly: If value = True, then the user cannot edit the field.
required: The value of this argument is True, then the field can’t be empty before saving.
size: The size of the field in the database can specify as a character or integer.
string: Set label for the field.
translate: If the value is True, then the content of the field should be translated.
invisible: Hide fields from view if the value is 1.
relation: Used to refer another table. Most commonly used in related and function field types.
compute: used to call a function to get the value of the field. The value of the compute will be the name of a function.Refer our technical blog that will help you to set a preferred view for a many2many field: Many2Many Fields and Its Widgets in Odoo