Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
3 Vastaukset
6776 Näkymät

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
Hylkää
Paras vastaus

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
Hylkää
Tekijä

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

Paras vastaus

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
Hylkää
Aiheeseen liittyviä artikkeleita Vastaukset Näkymät Toimenpide
1
huhtik. 19
9939
1
maalisk. 22
11604
1
syysk. 21
5608
5
syysk. 19
6248
0
maalisk. 20
3374