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:
- Managing the data structure to be stored in the database.
- Define relationships between data (e.g. the relationship between customers and invoices).
- 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:
- Import some code from odoo, namely models, fields, and api
- Inheriting the Models class from odoo, or you could say making a derivative
- _name is the model name
- _description is a description of the model
- Fields in the model
- 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 Type | Description | Usage Examples |
fields.Char | Short text (max. 255 characters) | Customer name, product code |
fields.Text | Long text | Product Description |
fields.Integer | Round number | Stock quantity |
fields.Float | Decimal numbers | Product prices, discounts |
fields.Boolean | True/false value | Active/inactive status |
fields.Date | Date | Date of birth |
fields.Datetime | Date and time | Transaction date |
fields.Selection | Selection from a list of fixed values | Status pesanan (Draft, Confirmed, Done) |
b. Relational Fields
Tipe Field | Description | Usage Examples |
fields.Many2one | Relationship to one other record | Customer on invoice |
fields.One2many | Relationship to many other records | List of products in order |
fields.Many2many | Relationship to multiple records without limitation | Product categories, user access rights |
c. Specific Fields
Field Type | Description | Usage Example |
fields.Html | Saving HTML data | Description with rich text format |
fields.Binary | Binary file | Product images, file attachments |
fields.Monetary | Currency 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! 🎉