Use [] to find tag! Example: [flutter, javascript]

Odoo Model Explanation

Odoo Model Explanation

Artikel ini dibukukan pada buku Odoo ERP Development
access_time 20 January 2025 remove_red_eye 1111 Kali spellcheck 464 Kata, 3672 Karakter
#odoo #python #postgre

Odoo is a highly flexible and Python-based ERP framework that allows easy development of business applications. One of the key components in Odoo development is Model, which is responsible for handling data and business logic. This article will explain what a model is, the anatomy of a model, and the different types of fields used in Odoo.

1. What is a Model?

Model in Odoo is a data representation of an entity or table in a database. Model is responsible for defining data structure, relationships between data, and the behavior of the data in the system.

For example, if you want to manage customers, you can create a model named res.partner that stores information such as name, email, phone number, and so on.

Main Functions of the Model:

  1. Managing the data structure to be stored in the database.
  2. Define relationships between data (e.g. the relationship between customers and invoices).
  3. Provides business logic related to those entities (such as validation or automated actions).

2. Anatomy of Model

Every model in Odoo is a subclass of models.Model and has several main elements. Here is the basic anatomy of an Odoo model:

  1. Import some code from odoo, namely models, fields, and api
  2. Inheriting the Models class from odoo, or you could say making a derivative
  3. _name is the model name
  4. _description is a description of the model
  5. Fields in the model
  6. Method or function in the model

3. Fields in Odoo Model

Fields are an important element in a model. Fields are used to determine the type of data stored in the database. Here are some types of fields that are often used in Odoo:

a. Basic Fields:

Field TypeDescriptionUsage Examples
fields.CharShort text (max. 255 characters)Customer name, product code
fields.TextLong textProduct Description
fields.IntegerRound numberStock quantity
fields.FloatDecimal numbersProduct prices, discounts
fields.BooleanTrue/false valueActive/inactive status
fields.DateDateDate of birth
fields.DatetimeDate and timeTransaction date
fields.SelectionSelection from a list of fixed valuesStatus pesanan (Draft, Confirmed, Done)

b. Relational Fields

Tipe FieldDescriptionUsage Examples
fields.Many2oneRelationship to one other recordCustomer on invoice
fields.One2manyRelationship to many other recordsList of products in order
fields.Many2manyRelationship to multiple records without limitationProduct categories, user access rights

c. Specific Fields

Field TypeDescriptionUsage Example
fields.HtmlSaving HTML dataDescription with rich text format
fields.BinaryBinary fileProduct images, file attachments
fields.MonetaryCurrency value

Product prices in specific currencies

Properti pada Fields

Each field can have additional properties to define data behavior and validation:

string: Labels for fields.

required: Are the fields required.

default: Default field values.

readonly: Is the field read-only.

help: Additional help or description for users.

Example Field with Properties:

name = fields.Char(string="Name", required=True, help="Nama lengkap pelanggan")
price = fields.Float(string="Price", default=0.0, required=True)
is_available = fields.Boolean(string="Available", default=True)

Conclusion

Models in Odoo are the foundation of data management and business logic. By understanding the anatomy of models and the types of fields available, you can design applications that are efficient and meet business needs. Fields provide the flexibility to handle various data types, while logic in models allows you to apply business rules easily.

If you are just starting out, make sure to understand these basic concepts before moving on to more complex features like views, controllers, and other modules. Happy coding! 🎉

Artikel ini dibukukan pada buku Odoo ERP Development
Navigasi Konten