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

Hello,

In Odoo 15, I have a form view with the following code to make the company_id field mandatory:

<xpath expr="/form/sheet//field[@name='company_id']" position="attributes"> <attribute name="required">1</attribute> </xpath>

This works as expected.

Now, I want to automatically populate the company_id field with the current user's company when creating a new record, using XML only. I've tried adding the following:

<xpath expr="/form/sheet//field[@name='company_id']" position="attributes"> <attribute name="required">1</attribute> <attribute name="context">{'default_company_id': company_id}</attribute> </xpath>

However, this does not auto-fill the field as intended.

Is there a way to achieve this purely through XML in Odoo 15? Or is it necessary to implement this functionality using Python code?

아바타
취소
베스트 답변

Hi,


Options you have,

1- Pure XML (without Python)

       You can only pass a static ID in the context attribute, e.g.:

                <field name="company_id" context="{'default_company_id': 1}"/>


-That will always default to company with ID 1, not the current user’s company.

-If your use case is always one company, this works.


2- Dynamic Default (Python needed)


    To use the current user’s company, you need Python because only Python can evaluate self.env.user.company_id.id.


    Example:


              company_id = fields.Many2one(

    'res.company',

    string="Company",

    default=lambda self: self.env.user.company_id.id,

    required=True,

)


- This ensures that whenever a new record is created, the company field is prefilled with the user’s company.


* With XML alone, you can only set a static default company.

* If you want the company to be the current user’s company, you must use Python (field default or default_get).


Hope it helps

아바타
취소
관련 게시물 답글 화면 활동
1
3월 25
1960
0
11월 24
1844
0
6월 24
1821
1
6월 24
2337
1
12월 23
1708