跳至内容
菜单
此问题已终结
3 回复
6780 查看

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
9939
1
3月 22
11606
1
9月 21
5609
5
9月 19
6249
0
3月 20
3374