콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다

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()
 


아바타
취소

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

작성자

Just tried it, not working...

베스트 답변

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.

아바타
취소
작성자

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.

작성자

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.

작성자 베스트 답변

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.

아바타
취소
베스트 답변

Has anyone found a solution to this issue yet?

아바타
취소
관련 게시물 답글 화면 활동
0
9월 21
2271
1
9월 23
4726
1
12월 23
17917
1
10월 16
10677
2
3월 15
4463