Index:
Custom Models
In Odoo we can create customs Models for our custom Addons. So in order to create a new model we need to navegate to the Custom addon following this route: Odoo 17.0 > server > odoo > customAddons > {Your Model} > models > models.py This file contains all the models that this addons have associated to, so if i wanna create a new model, i need to modify this file. This file contains all we need to create the models for our addon.
Transclude of models.pyThe code is also Here!
We have 2 classes called the same “ies”, but they are different, the first one creates a model called “ies.curso” which “ies” is the model and “curso” is the model’s name. The second class is called “ies.profesores”, “ies” is the model and “profesores” is the model’s name. The attributes of both classes are nearly the same, we got:
-
_name : this variable holds the name of the module, this name is UNIQUE
-
_description : this variable holds the description of the module, like information about the module.
this 2 are a must to have in the code, or else, odoo can’t build the module correctly or it will throw a big error._ Now its time to code the fields, its simple, we create a variable with he name we want and then we initialize it like this:
- If its a String:
{field_Name} = fields.Char(String="Nombre del profesor", required=True, help="Introduce el nombre del curso", size=40) - If its a Boolean:
{Field_Name} = fields.Boolean(String="Activo") - If its a Selection:
{Field_Name} = fields.Selection( [('01', '{Values}'), ('02', '{Value}'), ('03', '{Value}')], default='03', <-- This is the default **KEY**, nothing more needed. String="{Description of the field}") - If its a Many2one:
{Field_Name} = fields.Many2one('{Model, example: res.lang}', string="{description of the field}", required=True, help="{Help box information}")
Now we save the file > restart the odoo service > head to Odoo > Aplication > and update aplication.
Search for our module, and “Activar”
To see if we done things right, we need to check the Models in odoo.
Here we see the modules that we created, i have more than 2 cuz i was testing.
Custom Views
Good, now its time to make the views, the code is Here! Lets break down the code, we got:
- The XML to create the Tree view of Cursos
- The XML to create the Form view of Cursos
- The XML to create the Tree view of Profesores, in my case i changed “Profesores” to “Turbo”
- The XML to create the Form view of Turbo
- The XML of the Action Window for Cursos
- The XML of the Action window for Turbo
- The XML for the Menu Items, which are created like this:
- A parent manu called whatever you want with the following id: “
{model_name}.menu_root” - A submenu called whatever you want with the following id: “
{model_name}.menu_1” and with a extra tribute that referes to the parentparent="{model_name}_menu_root" - Submenus that extends from the submenu “
{model_name}.menu_1” with attributes like:- “
<menuitem name="{name}" id="{model_name}.menu_1_list" parent="{model_name}.menu_1" action="{model_name}.curso_action_window"/>” - “
<menuitem name="{name}" id="{model_name}.menu_2_list" parent="{model_name}.menu_1" action="{model_name}.turbo_action_window"/>” You can see my code is you have any doubts about it here.
- “
- A parent manu called whatever you want with the following id: “
Now the most important thing, well they’re all important but this one is important if we wanna see the menus, we need to go to security folder of our Custom Addon and modify the CSV file in order to create Permissions. So Here you have the CSV with permission created for both Models. The structure of the CSV is like the following:
| id | name | model_id_id | group_id_id | perm_read | perm_write | perm_create | perm_unlink |
|---|---|---|---|---|---|---|---|
| perm | CPermissions | model_xwd_curso | --- | 1 | 1 | 1 | 1 |
| permTurbo | TPermissions | model_xwd_turbo | --- | 1 | 1 | 1 | 1 |
Here I’ve made a table to visualize it better.
