Help

1

Passing context to a tree view field inside a form view

Avatar
Maximilien Celis

Hi all,

I'm struggling to pass a context to a tree view field, which is nested inside a form view. Not 100% certain but I'm pretty sure I've done this successfully before. Is it due to Odoo V14 ?

When I create a new credit, i would like the building field to be populated before saving the record.

My form view :

<record model="ir.ui.view" id="bld_building_form_view">
    <field name="name">bld.building.form</field>
    <field name="model">bld.building</field>
    <field name="arch" type="xml">
    <form string="Building">
...
        <field name="id"/>
        <field name="credits" string="Credits" context="{'default_building_id': id}">
            <tree string="Credits">
                <field name="name"/>
                <field name="amount" widget="monetary" sum="Total"/>
                <field name="interest_rate" string="Annual interest rate %"/>
                <field name="start_date"/>
                <field name="building_id"/>
            </tree> 
        </field>
...

My model :

class Building(models.Model):
    _name = 'bld.building'
    _inherit = ['mail.thread', 'mail.activity.mixin']
    _description = 'Building'

credits = fields.One2many('bld.credit', 'building_id', string='Credits', copy=True, auto_join=True)

class Credit(models.Model): _name = 'bld.credit' _description = 'Credit' building_id = fields.Many2one('bld.building', string='Building', required=True, index=True, copy=True) name = fields.Char(required=True) amount = fields.Monetary(store=True) interest_rate = fields.Float(string='Annual interest rate', store=True) start_date = fields.Date()
 


2 Opmerkingen
Avatar
Negeren
Avatar
Travis Waelbroeck
-

You might try using `context="{'default_building_id': active_id}">`

Avatar
Maximilien Celis
-

Just tried it, not working...

7 Antwoorden
0
Avatar
Maximilien Celis
Beste antwoord

Update :

I just build a basic module with a similar model and view structure on both Odoo 13.0 and 14.0. I can now confirm that it works fine on 13.0 and not on 14.0. Seems like Odoo changed the context approach on 14.0.

Question remains if it is still possible in 14.0 to populate a many2one field without having to save the record first.

Avatar
Negeren
0

Why do you want to set buidling_id as default in O2m?

Odoo by default passes the reference m2o fields in o2m as soon as you save the record. You don't need to pass it forcefully.

Remove the "<field name="building_id"/>" from the xml and try it again.

3 Opmerkingen
Avatar
Negeren
Avatar
Maximilien Celis
-

Good point, but I need the field to be populated before saving. On another field I have a domain filter tied to building_id.

Are you creating the lines after saving the main record or creating everything in one go?

If you are saving the main record first and then creating the lines, I think you will see the value in building_id filed as default.

Avatar
Maximilien Celis
-

Thanks for your help. The main record (Building) is already created and saved.

However when I add a line (Credit), the building_id of that line is not populated by default. It only populates when I save the line. I'm trying to have it populated before saving.