Help needed in Domain creation

I have this code of Renumeration Bill model which has a field “exam”

 exam = fields.Many2One(
    'exam_section.exam',
    'Exam'
)

This model(exam_section.exam) has an attribute called “exam_type”. Now I have a One2Many field called “purpose” in Renumeration Bill model

purpose = fields.One2Many(
    'exam_type.purpose_and_pay_renum',
    'renumeration',
    'Purpose')

Now in the model “exam_type.purpose_and_pay_renum”, I have a field called “purpose” which goes something like this

purpose = fields.Many2One(
    'exam_section.exam_type.renumeration',
    'Purpose',
    domain=[
        ('exam_type', '=', )
    ]
)

Now in this domain I want to fetch the “exam_type” of the “exam” field in Renumeration Bill.
I have tried the following

Eval('renumeration.exam.exam_type')

and

Eval(Eval(Eval('renumeration').get('exam')).get('exam_type'))

and

Eval('renumeration').exam.exam_type

and

RenumerationBill(Eval('renumeration')).exam.exam_type

None of them seem to work. I want to make a domain that correctly fetches the exam_type.
The model ‘exam_section.exam_type.renumeration’ also has the attribute ‘exam_type’

Some help is appreciated

For now Eval does not support domain inversion, so you should create a functional field on your model that returns the value of the exam_type of your parent model. Something like:

exam_type = fields.Function(fields.Many2One(), 'on_change_with_exam_type')

@fields.depends('renumeration'):
def on_change_with_exam_type(self, name=None):
    if self.renumeration and self.renumeration.exam:
        return self.renumeration.exam.exam_type.id

Hope it helps!

This should be improved for new versions when Issue 8528: Support dot notation on eval - Tryton issue tracker is resolved. Which will allow using the following syntax:

3 Likes

Thanks!
It works, however, I have to save the record in order for the domain to work. What do I need to do in order to avoid saving the record first?

Currently testing this using SAO

But it will not allow to traverse Many2One except via the _parent_ attribute if it is inside a One2Many.

That’s because you are probably missing some value on the depends decorator of the on_change_with, which makes the function to not be called when updating the parent field.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.