Odoo model is used to create a database table and its attributes (columns). Odoo uses Object Relational Mapping, that is the major feature of Odoo. ORM means that you can play with SQL queries without writing any SQL query by hand. Odoo provides an ORM layer to play with the database using python classes that are called model in Odoo.
Create a New Model in Odoo13
Whenever we create a model in Odoo, a database table will be generated in PostgreSQL. Every model has some fields, and creating fields means, creating table columns or attributes. Remember you can declare as many fields as per your project requirement. Below are the types of the field which you can declare for your model.
- Char
- Float
- Integer
- Boolean
- Selection
- Text
- Date
- Datetime
- Many2one
- One2many
- Many2many
Code Description
from odoo import models, fields, api
First of all we have to import models and fields from odoo, models are business objects (python class) that extends Model. After that we have defined a class named ModelA, with name as model.a. This will create a new table in our PostgreSQL database. To generate columns in table we have defined multiple fields like Char, Text, Date, Selection and Float etc. Odoo fields has multiple attributes as given in table below.
Recommend:
Attribute | Value |
---|---|
string | Field name that will be displayed on form and tree view (Any String) |
required | Field value is required, must be entered (True or False) |
default | To set default value for a give field (True or False or Some Fuction) |
ondelete | To restrict or cascade relational field (Many2one) |
compute | To make a field computable (To calculate some value like, cgpa, total amount etc) |
store | Store computed field value into the database (Generally computed field value not stored into the database) |
domain | Get specific records in relational field like (Many2one, Many2many etc) |
related | Create same field in current model that exists in some another model |
Now restart the Odoo server and upgrade module. To check that database table and columns are created or not, open PostgreSQL SQL query editor and run the below query.
select * from model_a
0 Comments