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

Hello everyone,

I am using Odoo 12 and need to check for a condition that is based on a child field from a "many2one" relation.

What I have:

    class Model1(models.Model):
        _name = 'model1'
        _description = 'This is the model1'

        code = fields.Char('Model 1 Code', required=True)
        name = fields.Char('Model 1 Name', required=True)
        reference_required = fields.Boolean('Requires a Reference', default=False)

    class Model2(models.Model):
         _name = 'model2'
         _description = 'This is the model2'

         #some fields here
         relation_to_model1_id = fields.Many2one('model1',)

What I am trying to do, is to check if the "reference_required" field on "model1" is true and if so, "relation_to_model1_id" field is shown.

Tried with no luck:

     <field name="relation_to_model1_id" attrs="{'invisible': [('relation_to_model1_id.reference_required','=',False)]}"/>

How can I achieve this on a form view?
I cannot even add a child field to the "model2" form view, using for instance:

     <field name="relation_to_model1_id.code"

Can anyone help achieve this?

Thank you in advance

PM

아바타
취소
베스트 답변

Have you tried adding a related field in Model2, i.e.

model1_id_reference_required = fields.Boolean(related='relation_to_model1_id.reference_required')

Then you can add it to the view and use it domain filters.

For reference  https://www.odoo.com/documentation/13.0/reference/orm.html#reference-fields-related

아바타
취소
작성자

Dear @Alessandro Fiorino,

Thank you very much (and all others also).

You were right. The method you described solved my problem.

I have tried later but having an inconsistency error because I was using the wrong field type on the destination related field and the origin field.

Problem solved.

Best regards

PM

베스트 답변

Hi Paulo:

Something does not seem right with the example. You are self-referencing in the attrs attribute of relation_to_model1_id. The value of relation_to_model1_id.reference_required cannot be determined till the value of relation_to_model1_id is set. 

아바타
취소
관련 게시물 답글 화면 활동
1
4월 19
9986
1
3월 22
11699
1
9월 21
5665
5
9월 19
6307
0
3월 20
3412