Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

Create a dynamic form view

By
Eduardo BP
on 5/9/15, 2:11 PM 1,409 views

Hi Odoo developers,

I have a model named 'TypeTask' which has a Char field called 'name_task'. This field represents the name of the task type (e.g call, meeting, visit, email, ...).

I have a menu associated with an action to open the form view of the TypeTask model This form only shows the field 'name_task'.

Then, I want that when the user is going to choose a value from this field in the form, new fields appear in the same form. These new fields depend on the task type chosen.
For instance, if the user chose the 'call' task type, the fields that should appear would be: 'telephone', 'day', 'hour', 'call duration', etc.
If the user chose the 'meeting' task type, the fields that should appear would be: 'place', 'assistants', etc.

I have implemented a model for each task type (model Call, model Meeting, model Visit, model Email). Each one inherits from the Task parent model. 
Also, I have a different form view for each ot these models.
The expected behaviour would be that when the user had chosen the 'call' task type in the form, the form view of the Call model would be opened in the Task Type form adding all fields of the Call model.

How can I get this behaviour in the form mentioned above?.

Thanks in advance.
-----------------------------
Odoo: version 8

OS: Ubuntu 14.04

I can't comment your answer Robert because I don't have enough karma. Thanks for your answer @Robert. I thought the same as you, but I would rather have an abstract class Task and childs classes inheriting from Task for the different kinds of Tasks (subclass Call, subclass Meeting, etc) using _inherit in my model.py file. Each subclass should have its own form view and each view should be opened when the user choose a kind of Task from the form view of the TypeTask model. The key is how to load dinamically a form view of a model from the form view of other module.

Eduardo BP
on 5/9/15, 8:07 PM
1
Eduardo BP
On 5/10/15, 1:50 PM

Hi Odoo developers,

I'm going to answer myself.

In my module I have a Course and a Session model. A course could be composed of zero, one or many sessions.
The Course model has associated a form view:

<record model="ir.ui.view" id="course_form_view">
<field name="name">course.form</field>
<field name="model">openacademy.course</field>
<field name="arch" type="xml">
<form>
<sheet>
<group>
<field name="name"/>
<field name="responsible_id" string="Responsible"/>
</group>
<notebook>
<page string="Description">
<group>
<field name="description"/>
</group>
</page>
<page string="Ayuda">
<p>Another <strong>tab</strong></p>
</page>
<page string="Sessions">
<field name="session_ids">
<tree string="Course sessions">
<field name="name"/>
<field name="instructor_id"/>
</tree>
</field>
</page>
</notebook>
</sheet>
</form>
</field>
</record>

The session model has associated another 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>
<header>
<button name="draft" type="workflow"
string="Reset to draft"
states="confirmed,done"/>
<button name="confirm" type="workflow"
string="Confirm" states="draft"
class="oe_highlight"/>
<button name="done" type="workflow"
string="Mark as done" states="confirmed"
class="oe_highlight"/>
<button type="action"
name="openacademy.action_new_course1"
string="New course"
confirm="Do you want to create a new course?"/>

<field name="state" widget="statusbar"/>
</header>
<sheet>
<group string="General">
<field name="name" string="Session name"/>
<field name="course_id" string="Course"/>
<field name="instructor_id" string="Instructor"/>
</group>
<group string="Management">
<field name="start_date" string="Start date"/>
<field name="active" string="Activo"/>
<field name="duration" string="Duration (in days)"/>
<field name="seats"/>
<field name="percent_taken_seats" widget="progressbar"/>
</group>
<label for="attendees"/>
<field name="attendees"/>
</sheet>
</form>
</field>
</record>

In the above view there is a button with the string attribute equals to "New course":

<button type="action" name="openacademy.action_new_course1" string="New course" 
    confirm="Do you want to create a new course?"
/>

The button is associated with a window action using the attribute name="openacademy.action_new_course1".
This window action is defined in the same .xml file that this button:

<record id="action_new_course1" model="ir.actions.act_window">
<field name="name">Course</field>
<field name="res_model">openacademy.course</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">current</field>
</record>

The above window action open the form view defined for the Course model mentioned before.
Apart from this, you can have others window actions bind to the same Course model with differente values for view_mode. For example, I have another window action associated to the Course model:

<record model="ir.actions.act_window" id="action_openacademy_list_courses">
<field name="name">Courses Manager</field>
<field name="res_model">openacademy.course</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form,calendar</field>
<field name="context" eval="{'search_default_my_courses': 1}"/>
</record>

And this window action is bind to a menu item to open the Course model view:

<menuitem id="courses_menu" name="My Courses"
parent="openacademy_menu"
action="action_openacademy_list_courses"
/>

When I click on the menu named "My Courses", a tree view corresponding to the Course model is opened, and I have two more buttons to open the form and the calendar views associated to this model.

<record model="ir.actions.act_window" id="action_openacademy_list_courses">
<field name="name">Courses Manager</field>
<field name="res_model">openacademy.course</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form,calendar</field>
<field name="context" eval="{'search_default_my_courses': 1}"/>
</record>

0
Dr Obx
On 5/9/15, 6:13 PM

Hi,

You talking about thing I already made some time ago. It's not exactly the same but idea is the same.

I needed a dynamically created form for products so depending on product type adequate list of fields or option is displayed.

In your case it may be a bit more complicated because you want switch between the views. I would do it that way:

(I'm not sure is the best idea but ....)

using "attrs" hide or show task dedicated fields.
in py file:
task_type = fields.selection([('task_1','taks one'),('task_2','task two'),('task_3','task three')],'Tasks'),

In xml:
<field name="task_type"/> # which let you choose the task you want.

and depending on task chosen task related group will be displayed :)
<group name="task_1" attrs={invisible:('task_type','!=','task_1')}>
...
<field name="_1"/>
<field name="_2"/>
...
</group> 

<group name="task_2" attrs={invisible:('task_type','!=','task_2')}>
...
<field name="_2"/>

<field name="_3"/>
...
</group>

I hope it help or just give you idea how you can solve it :)

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 5/9/15, 2:11 PM
Seen: 1409 times
Last updated: 5/10/15, 1:53 PM