Create a model for *sessions*. A session has a name, a start date, a
duration and a number of seats. Add an action and a menu item to display
- them.
+ them. Make the new model visible via a menu item.
.. only:: solutions
- Create the class *Session* in ``openacademy/models.py``.
+ #. Create the class *Session* in ``openacademy/models.py``.
+ #. Add access to the session object in ``openacademy/view/openacademy.xml``.
.. patch::
accessing it results in a (possibly empty) set of records::
for other in foo.other_ids:
- print foo.name
+ print other.name
.. danger::
records, accessing it also results in a possibly empty set of records::
for other in foo.other_ids:
- print foo.name
+ print other.name
.. exercise:: Many2one relations
built-in model ``res.partner``.
- A session is related to a *course*; the value of that field is a record
of the model ``openacademy.course`` and is required.
+ - Adapt the views.
.. only:: solutions
#. Add the relevant ``Many2one`` fields to the models, and
- #. add access to the session object in
- ``openacademy/view/openacademy.xml``.
+ #. add them in the views.
.. patch::
.. only:: solutions
- Modify the ``Course`` class as follows:
+ #. Modify the ``Course`` class, and
+ #. add the field in the course form view.
.. patch::
Using the relational field many2many, modify the *Session* model to relate
every session to a set of *attendees*. Attendees will be represented by
partner records, so we will relate to the built-in model ``res.partner``.
+ Adapt the views accordingly.
.. only:: solutions
- Modify the ``Session`` class as follows:
-
- .. patch::
-
-.. exercise:: Views modification
-
- For the *Course* model,
-
- * the name and instructor for the course should be displayed in the tree
- view
- * the form view should display the course name and responsible at
- the top, followed by the course description in a tab and the course
- sessions in a second tab
-
- For the *Session* model,
-
- * the name of the session and the session course should be displayed in
- the tree view
- * the form view should display all the session's fields
-
- Try to lay out the form views so that they're clear and readable.
-
- .. only:: solutions
+ #. Modify the ``Session`` class, and
+ #. add the field in the form view.
.. patch::
Index: addons/openacademy/models.py
===================================================================
---- addons.orig/openacademy/models.py 2014-08-26 17:25:59.483783379 +0200
-+++ addons/openacademy/models.py 2014-08-26 17:25:59.479783379 +0200
+--- addons.orig/openacademy/models.py 2014-08-27 10:34:08.531934946 +0200
++++ addons/openacademy/models.py 2014-08-27 10:34:08.527934946 +0200
@@ -25,3 +25,4 @@
instructor_id = fields.Many2one('res.partner', string="Instructor")
course_id = fields.Many2one('openacademy.course',
ondelete='cascade', string="Course", required=True)
+ attendee_ids = fields.Many2many('res.partner', string="Attendees")
+Index: addons/openacademy/views/openacademy.xml
+===================================================================
+--- addons.orig/openacademy/views/openacademy.xml 2014-08-27 10:34:08.531934946 +0200
++++ addons/openacademy/views/openacademy.xml 2014-08-27 10:34:08.527934946 +0200
+@@ -102,6 +102,8 @@
+ <field name="seats"/>
+ </group>
+ </group>
++ <label for="attendee_ids"/>
++ <field name="attendee_ids"/>
+ </sheet>
+ </form>
+ </field>
Index: addons/openacademy/models.py
===================================================================
---- addons.orig/openacademy/models.py 2014-08-26 17:25:57.515783409 +0200
-+++ addons/openacademy/models.py 2014-08-26 17:25:57.511783409 +0200
+--- addons.orig/openacademy/models.py 2014-08-27 10:37:10.135932250 +0200
++++ addons/openacademy/models.py 2014-08-27 10:37:10.131932250 +0200
@@ -8,6 +8,9 @@
name = fields.Char(string="Title", required=True)
description = fields.Text()
+ ondelete='cascade', string="Course", required=True)
Index: addons/openacademy/views/openacademy.xml
===================================================================
---- addons.orig/openacademy/views/openacademy.xml 2014-08-26 17:25:57.515783409 +0200
-+++ addons/openacademy/views/openacademy.xml 2014-08-26 17:25:57.511783409 +0200
-@@ -64,5 +64,16 @@
- <!-- Full id location:
- action="openacademy.course_list_action"
- It is not required when it is the same module -->
+--- addons.orig/openacademy/views/openacademy.xml 2014-08-27 10:37:10.135932250 +0200
++++ addons/openacademy/views/openacademy.xml 2014-08-27 10:37:10.131932250 +0200
+@@ -9,6 +9,7 @@
+ <sheet>
+ <group>
+ <field name="name"/>
++ <field name="responsible_id"/>
+ </group>
+ <notebook>
+ <page string="Description">
+@@ -34,6 +35,18 @@
+ </field>
+ </record>
+
++ <!-- override the automatically generated list view for courses -->
++ <record model="ir.ui.view" id="course_tree_view">
++ <field name="name">course.tree</field>
++ <field name="model">openacademy.course</field>
++ <field name="arch" type="xml">
++ <tree string="Course Tree">
++ <field name="name"/>
++ <field name="responsible_id"/>
++ </tree>
++ </field>
++ </record>
+
-+ <record model="ir.actions.act_window" id="session_list_action">
-+ <field name="name">Sessions</field>
-+ <field name="res_model">openacademy.session</field>
-+ <field name="view_type">form</field>
-+ <field name="view_mode">tree,form</field>
+ <!-- window action -->
+ <!--
+ The following tag is an action definition for a "window action",
+@@ -73,16 +86,34 @@
+ <form string="Session Form">
+ <sheet>
+ <group>
+- <field name="name"/>
+- <field name="start_date"/>
+- <field name="duration"/>
+- <field name="seats"/>
++ <group string="General">
++ <field name="course_id"/>
++ <field name="name"/>
++ <field name="instructor_id"/>
++ </group>
++ <group string="Schedule">
++ <field name="start_date"/>
++ <field name="duration"/>
++ <field name="seats"/>
++ </group>
+ </group>
+ </sheet>
+ </form>
+ </field>
+ </record>
+
++ <!-- session tree/list view -->
++ <record model="ir.ui.view" id="session_tree_view">
++ <field name="name">session.tree</field>
++ <field name="model">openacademy.session</field>
++ <field name="arch" type="xml">
++ <tree string="Session Tree">
++ <field name="name"/>
++ <field name="course_id"/>
++ </tree>
++ </field>
+ </record>
+
-+ <menuitem id="session_menu" name="Sessions"
-+ parent="openacademy_menu"
-+ action="session_list_action"/>
- </data>
- </openerp>
+ <record model="ir.actions.act_window" id="session_list_action">
+ <field name="name">Sessions</field>
+ <field name="res_model">openacademy.session</field>
Index: addons/openacademy/models.py
===================================================================
---- addons.orig/openacademy/models.py 2014-08-26 17:25:58.455783395 +0200
-+++ addons/openacademy/models.py 2014-08-26 17:25:58.447783395 +0200
+--- addons.orig/openacademy/models.py 2014-08-27 10:37:24.591932036 +0200
++++ addons/openacademy/models.py 2014-08-27 10:37:24.583932036 +0200
@@ -10,6 +10,8 @@
responsible_id = fields.Many2one('res.users',
class Session(models.Model):
+Index: addons/openacademy/views/openacademy.xml
+===================================================================
+--- addons.orig/openacademy/views/openacademy.xml 2014-08-27 10:37:24.591932036 +0200
++++ addons/openacademy/views/openacademy.xml 2014-08-27 10:37:24.583932036 +0200
+@@ -15,8 +15,13 @@
+ <page string="Description">
+ <field name="description"/>
+ </page>
+- <page string="About">
+- This is an example of notebooks
++ <page string="Sessions">
++ <field name="session_ids">
++ <tree string="Registered sessions">
++ <field name="name"/>
++ <field name="instructor_id"/>
++ </tree>
++ </field>
+ </page>
+ </notebook>
+ </sheet>
# Parent 22f8d180a7f9ad209d7e98cf7d1bd0fee1f05350
Index: addons/openacademy/models.py
===================================================================
---- addons.orig/openacademy/models.py 2014-08-26 17:25:56.603783422 +0200
-+++ addons/openacademy/models.py 2014-08-26 17:25:56.595783422 +0200
+--- addons.orig/openacademy/models.py 2014-08-27 10:35:29.179933749 +0200
++++ addons/openacademy/models.py 2014-08-27 10:35:56.000000000 +0200
@@ -7,3 +7,12 @@
name = fields.Char(string="Title", required=True)
+ start_date = fields.Date()
+ duration = fields.Float(digits=(6, 2), help="Duration in days")
+ seats = fields.Integer(string="Number of seats")
+Index: addons/openacademy/views/openacademy.xml
+===================================================================
+--- addons.orig/openacademy/views/openacademy.xml 2014-08-27 10:35:29.179933749 +0200
++++ addons/openacademy/views/openacademy.xml 2014-08-27 10:36:54.643932480 +0200
+@@ -64,5 +64,34 @@
+ <!-- Full id location:
+ action="openacademy.course_list_action"
+ It is not required when it is the same module -->
++
++ <!-- session form view -->
++ <record model="ir.ui.view" id="session_form_view">
++ <field name="name">session.form</field>
++ <field name="model">openacademy.session</field>
++ <field name="arch" type="xml">
++ <form string="Session Form">
++ <sheet>
++ <group>
++ <field name="name"/>
++ <field name="start_date"/>
++ <field name="duration"/>
++ <field name="seats"/>
++ </group>
++ </sheet>
++ </form>
++ </field>
++ </record>
++
++ <record model="ir.actions.act_window" id="session_list_action">
++ <field name="name">Sessions</field>
++ <field name="res_model">openacademy.session</field>
++ <field name="view_type">form</field>
++ <field name="view_mode">tree,form</field>
++ </record>
++
++ <menuitem id="session_menu" name="Sessions"
++ parent="openacademy_menu"
++ action="session_list_action"/>
+ </data>
+ </openerp>
exercise-many2one
exercise-one2many
exercise-many2many
-exercise-o2m-views
exercise-model-inheritance
exercise-domain-basic
exercise-domain-advanced