Let us start from scratch by creating a toy module called my_module
.
First, we need to create a model. The file addons/my_module/models/models.py
contains:
from odoo import api, fields, models, tools, _
class Books(models.Model):
_name = 'my_module.books'
title = fields.Char(string='Title')
author = fields.Char(string='Author')
Now, we are in position to create menus, actions and views. The file addons/my_module/views/views.xml
contains:
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<!-- Top menu item -->
<menuitem name="My top menu" id="my_module.menu_root"/>
<!-- menu categories -->
<menuitem name="My left-side menu" id="my_module.menu_left" parent="my_module.menu_root"/>
<!-- Model Books: views, actions and menus -->
<!-- explicit tree view definition -->
<record model="ir.ui.view" id="my_module.books_view_tree">
<field name="name">My books - List view</field>
<field name="model">my_module.books</field>
<field name="arch" type="xml">
<tree>
<field name="title"/>
<field name="author"/>
</tree>
</field>
</record>
<!-- explicit form view definition -->
<record model="ir.ui.view" id="my_module.books_view_form">
<field name="name">My books - Form view</field>
<field name="model">my_module.books</field>
<field name="arch" type="xml">
<form string="My books">
<sheet>
<group>
<field name="title"/>
<field name="author"/>
</group>
</sheet>
</form>
</field>
</record>
<!-- action opening views for this model -->
<record model="ir.actions.act_window" id="my_module.books_action_window">
<field name="name">My Books - Window action</field>
<field name="res_model">my_module.books</field>
<field name="view_mode">tree,form</field>
</record>
<!-- menu for the above action -->
<menuitem name="Books" id="my_module.menu_books" parent="my_module.menu_left" action="my_module.books_action_window" sequence="6" />
</data>
</openerp>
Then, we need a manifesto. The file addons/my_module/__manifest__.py
contains:
{
'name': "my_module",
'summary': "Answer to Stack Overflow Question 52733625",
'description': "Answer to Stack Overflow Question 52733625",
'author': "Adán Cortés Medina",
'website': "http://www.linkedin.com/in/1acme",
# Categories can be used to filter modules in modules listing
# Check https://github.com/odoo/odoo/blob/9.0/openerp/addons/base/module/module_data.xml
# for the full list
'category': 'Uncategorized',
'version': '0.1',
# any module necessary for this one to work correctly
'depends': ['website'],
# always loaded
'data': [
'views/views.xml',
#'security/ir.model.access.csv',
],
# only loaded in demonstration mode
'demo': [
#'demo/demo.xml',
],
}
At this point, you most likely want to create a new database and load the module. There will be a menu on top named My top menu
, then there will be a label on the left reading My left-side menu
and beneath there will be a menu reading Books
which, when you click on it, the list view for the books model will be shown. Also, when you click on the Create
button, you will be shown the form view for the said model.
Once that it is tested, let us add security. First, go to the __manifest__.py
file and uncomment #'security/ir.model.access.csv',
by removing the leading #
. Then create addons/my_module/security/ir.model.access.csv
with the following contents:
"id","name","model_id/id","group_id/id","perm_read","perm_write","perm_create","perm_unlink"
"access_my_module_books","my_module.books","my_module.model_my_module_books","base.group_portal","True","True","True","True"
Restart Odoo, just to be on the safe side and then go to the Apps
menu and reload your module by clicking first on its card and then on the Upgrade
button.
Now, enter to debug mode by adding debug
just before the #
(the URL should be like http://example.com:8096/web?debug#...) and create a user while making sure it belongs to the portal group (Other Extra Rights/Portal
must be ticked). Click on the Save
button. Look for the Action
drop-down menu on the top of the form and select Change password
. Write a password, click on Save
.
Then open a new incognito window (to be able to have two Odoo accounts simultaneously open) and log in with the user you just created. The Books menu should be shown there.
Finally, create a new user not belonging to the Portal group, log in with that user and check that the Books menu won't show.
You can download the aforementioned toy module from https://github.com/AdanCortes/stackoverflow/tree/q52733625