Storing data in Odoo
Odoo modelsmap to database tables.
In the previous section we just displayed a list of string entered statically in the Python code. This doesn't allow modifications or persistent storage so we'll now move our data to the database.
Defining the data model
Define a teacher model, and ensure it is imported from__init__.py
so it is correctly loaded:
academy/models.py
from odoo import models, fields, api
class Teachers(models.Model):
_name = 'academy.teachers'
name = fields.Char()
Then setupbasic access controlfor the model and add them to the manifest:
academy/\_manifest__.py_
# always loaded
'data': [
'security/ir.model.access.csv',
'templates.xml',
],
# only loaded in demonstration mode
this simply gives read access (perm_read
) to all users (group_id:id
left empty).
Note
Data files(XML or CSV) must be added to the module manifest, Python files (models or controllers) don't but have to be imported from__init__.py
(directly or indirectly)
Warning
the administrator user bypasses access control, they have access to all models even if not given access
Demonstration data
The second step is to add some demonstration data to the system so it's possible to test it easily. This is done by adding ademo
data file, which must be linked from the manifest:
academy/demo.xml
<odoo>
<data>
<record id="padilla" model="academy.teachers">
<field name="name">Diana Padilla</field>
</record>
<record id="carroll" model="academy.teachers">
<field name="name">Jody Carroll</field>
</record>
<record id="vaughn" model="academy.teachers">
<field name="name">Lester Vaughn</field>
</record>
</data>
</odoo>
Tip
Data filescan be used for demo and non-demo data. Demo data are only loaded in "demonstration mode" and can be used for flow testing and demonstration, non-demo data are always loaded and used as initial system setup.
In this case we're using demonstration data because an actual user of the system would want to input or import their own teachers list, this list is only useful for testing.
Accessing the data
The last step is to alter model and template to use our demonstration data:
- fetch the records from the database instead of having a static list
- Because
search()
returns a set of records matching the filter ("all records" here), alter the template to print each teacher'sname
academy/controllers.py
class Academy(http.Controller):
@http.route('/academy/academy/', auth='public')
def index(self, **kw):
Teachers = http.request.env['academy.teachers']
return http.request.render('academy.index', {
'teachers': Teachers.search([])
})
# @http.route('/academy/academy/objects/', auth='public')
academy/templates.xml
<template id="index">
<title>Academy</title>
<t t-foreach="teachers" t-as="teacher">
<p><t t-esc="teacher.id"/> <t t-esc="teacher.name"/></p>
</t>
</template>
<!-- <template id="object"> -->
Restart the server and update the module (in order to update the manifest and templates and load the demo file) then navigate to
http://localhost:8069/academy/academy/
. The page should look slightly different: names should simply be prefixed by a number (the database identifier for the teacher).