This question has been flagged
3 Replies
5545 Views

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

Avatar
Discard
Best Answer

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

Avatar
Discard
Author

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

Best Answer

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. 

Avatar
Discard