This question has been flagged
3 Replies
4236 Views

Hi,

I have these models :
*****************************************

class MgCourse(models.Model):
    _name = 'ts.course'
    _description = 'Course'

    name = fields.Char('Course Name', size=128, required=True)
    desc = fields.Text('Course Description')
    batches = fields.One2many('ts.course.batch', 'batch_course_id',
     'batches for this course (test)', String='Batches')

*****************************************

class MgBatch(models.Model):
    _name = 'ts.course.batch'
    _description = 'Batch'

    name = fields.Char('Batch Name', size=128, required=True)
    batch_course_id = fields.Many2one('ts.course', 'Course', ondelete="cascade")

*****************************************

I created a form for Course model where the "batches" field is shown in tree view. By clicking at "create new item" there, I get a form of batch model.
The problem is that the form (the batch form) contains the field "batch_course_id".

In this batch form:

1- I dont want that the field batch_course_id be displayed because we already know the corresponding course of this batch. 2- The value of batch_course_id in this form should automatically be the id of the original Course from where we opened the form.

Please, is there any way to make the new batch (created from the course form) implicitly point to the original course ?
I don't want to remove the field batch_course_id from ts.batch because I need it in the batch form view.

Thank for any piece of answer.


Avatar
Discard
Author Best Answer

I've resolved the problem by customizing the course form view like this :

<form string="Course">
<sheet>
<notebook colspan="4">
<page name="general_information" string="General Information">
<group colspan="4" col="4" name="main">
<field name="name" />
<field name="desc" />
</group>
</page>
<page name="batches" string="Batches details">
<group string="Batches" name="batch">
<field name="batches_ids">
<tree>
<field name="name"/>
</tree>
<form string="Transport Vehicles">
<group>
<field name="name"/>
</group>
</form>
</field>
</group>
</page>
</notebook>
</sheet>
</form>

By this way, the field batch_course_id is automatically set by odoo.

Avatar
Discard
Best Answer

Hi,

As far as i understood if you want to get the course_id in the one2many you can use the context and default.

This is a sample try like this,

<record model="ir.ui.view" id="sale_transport_form">
<field name="name">sale.order</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='partner_id']" position="after">
<field name="transport_from"/> #this value has to be passed to the one2many
</xpath>
<xpath expr="//page[1]" position="after">
<page string="Transport Information">
                #in the context the value is passing  
<field name="transport_line" context="{'default_transport_from': transport_from}" >
<tree>
<field name="transport_from"/>
<field name="vehicle_number"/>
<field name="vehicle_license"/>
</tree>
<form string="Transport Vehicles">
<group>
<field name="transport_from"/>
<field name="vehicle_number"/>
<field name="vehicle_license"/>
             </group>
</form>
</field>
</page>
</xpath>
</field>
</record>


Thanks

Avatar
Discard
Author

Thank you, Niyas Raphy.

As I understand in your example:

default_transport_from corresponds to batch_course_id and

transport_from corresponds to the id of newly created course.

But how can I get the id of newly created course (its creation is not submitted yet) ?