Read More
Read More
Menu Close

    ORM API: Odoo Fields

    Odoo is basically a web-based open source & modular-based framework/software which include multiple business solutions like “CRM”, “POS”, “E-Commerce”, “ERP”, “Manufacturing”, “Inventory Management”, “Billing & Accounting” and “Project Management”.

    Also it follows the ORM structure.

    In our last blog we studied how to create a new module in Odoo.

    As we mentioned above, Odoo follows the ORM(“Object Relational Mapping”) structure which manipulates data from the database into Object-Oriented form. 

    Classes are represented as tables and fields are represented as columns in Odoo.

    Start your headless eCommerce
    now.
    Read More

    So in this article we will talk about Odoo fields.

    What is the Odoo field?

    Odoo is a modular based framework.

    So everything is managed by modules and classes.

    Odoo Fields are managed by field class.

    Attributes of Odoo Fields

    1. string: It represents the label of Odoo fields eg: string=” Webkul”
    2. help: It represents the description of Odoo fields eg: help=” This is Webkul type field”
    3. readonly: If true then the Odoo fields not editable eg: readonly=True/False
    4. invisible: If true then the Odoo fields not visible to the user eg: invisible=True/False
    5. required: If true then the Odoo fields compulsory for the user eg: required=True/False
    6. copy: If true then the record will be copied if any record is duplicate eg: copy=True/False
    7. index: If true then the field is indexed in database eg: index=True/False
    8. store: If true then the field is stored in the database(For computed/related fields only) eg: store=True/False
    9. default: It is used to set the value of the field, If a value is passed inside this attribute then during record create that value will be set inside the record.
    10. group: If the comma-separated list of groups XML ids passed then it will restrict the users of these groups to access these fields.

    And there are many other attributes which are present with the Odoo fields.

    Where are these fields stored in Odoo?

    In Odoo fields are also stored in an object form. All the fields of Odoo are stored inside the “ir.model.fields” and you can also find the list of fields associated with the models which are also present as records in Odoo inside the “ir.model” model.

    How many types of fields in Odoo?

    In Odoo data is stored in the simple, relational, and computed form.

    Currently, in Odoo, there are three major types of fields which are given below,

    Basic Fields

    It contains the fields which are stored in the simplified form.

    Boolean

    This field stores value in 0/1(False/True) and represented by class Boolean.

    If false then it stores 0 and if true then value will be 1.

    eg: field_name = fields.Boolean(string=”Field Label”)

    Character

    This field stores value in string form and represented by class Char.

    eg: field_name = fields.Char(string=”Field Label”)

    Integer

    This field stores value in numeric form and represented by class Integer.

    eg: field_name = fields.Integer(string=”Field Label”)

    Float

    This field stores value in decimal form and represented by class Float.

    eg: field_name = fields.Float(string=”Field Label”)

    Advanced Fields

    Presentable

    Binary

    This field stores value in binary form.

    It store the attachments like file, images, musical files, etc, and represented by class Binary.

    eg: field_name = fields.Binary(string=”Field Label”)
    HTML

    This field stores value in string form and it is used to present the data in html form. HTML field is represented by class HTML. Parameters like sanitize, sanitize_tags, sanitize_style etc, are used inside the html fields,

    eg: field_name = fields.HTML(string=”Field Label”)
    Image

    This field stores value in binary form and used to present the data in image form. The image field is inherited by binary class and represented by class Image. Parameters like max_width, max_height etc, are used inside the image fields,

    eg: field_name = fields.Image(string=”Field Label”)
    Monetary

    This field stores value in decimal form and used to present the money along with the currency and represented by class Monetary. Parameters currency field(a many2one field of res.currency object) etc and used inside the monetary fields,

    eg: field_name = fields.Monetary(string=”Field Label”)
    Selection

    This field stores value in string form and this field helps to provide the selection to the user. Selection field is represented by class Selection. It uses the below attribute to represent the selection to the user,

    • selection = [(‘a’, ‘A’), (‘b’, ‘B’)]
    • selection_add = [(‘c’, ‘C’), (‘b’,)]
    eg:field_name = fields.Selection([(‘a’, ‘A’), (‘b’, ‘B’)], string=”Field Label”, default=’a’)

    Text

    This field stores value in string form. Basically this field helps to store the long text at Odoo end and represented by class Text.

    eg: field_name = fields.Text(string=”Field Label”)

    Date(Time)

    Date

    This field stores value in object form and used to present the date view to the user. Which helps the user to select the date and represented by class Date.

    eg: field_name = fields.Date(string=”Field Label”)
    Datetime

    This field stores value in object form. It present the datetime view to the user. This helps the user to select the date and time as well and represented by class Datetime.

    eg: field_name = fields.Datetime(string=”Field Label”)

    Relational Fields

    It contains the fields which provide the relation between the two tables and represented by the Relational class.

    Many2one

    This field stores recordset(0 or 1 record for the related table). The “comodel_name” attribute helps to define the target model and represented by class Many2one.

    eg: field_name = fields.Many2one(comodel_name=”webkul.webkul”, string=”Field Label”)

    Many2many

    These field store recordsets (0 or 1 or more records for the related table). “comodel_name” attribute helps to define the target model and represented by class Many2many.

    eg: field_name = fields.Many2many(comodel_name=”webkul.webkul”, relation=”webkul_software_rel” string=”Field Label”)

    One2many

    These field store recordsets (0 or 1 or more records for the related table). “comodel_name” attribute helps to define the target model and represented by class One2many.

    eg: field_name = fields.One2many(comodel_name=”webkul.webkul”, inverse_name =”webkul_id” string=”Field Label”)

    Related Fields

    Dynamically typed field.

    It stores the subfield of tables. It uses the related attribute.

    eg: field_name = fields.Char(related=”webkul_id.name”, string=”Field Label”)

    Computed Fields

    This field helps to store value as per the requirement. This field uses the compute attribute.

    eg: field_name = fields.Char(compute=”get_data”, string=”Field Label”)

    We Would Love to Hear From You!

    Hope you find the blog informative! Please feel free to share your feedback in the comments below.

    Also, check our store page to go through our other modules.

    If you still have any issues/queries then please raise a ticket at https://webkul.uvdesk.com/en/customer/create-ticket/

    For any doubt contact us at [email protected].

    Thanks for paying attention!!

    🙂 😊

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top