How to Query model id from a function?

I have below code:

class RAGQuotationReportForm(ModelView):
    'RAG Quotation Report Start'
    __name__ = 'ltl.crm.rag.quotation.report.form'

    quotation = fields.Many2One('ltl.crm.rag.quotation', 'Quotation', required=True)

    subject = fields.Char('Subject', readonly=True)
    to_company = fields.Char('To Company', readonly=True)
    to_company_address = fields.Many2One('ltl.company.address', 'To Company Address',
        domain=[
            ('company', '=', Eval('company_id'))
        ],
    )

    company_id = fields.Function(fields.Many2One('ltl.company', 'Company ID'),'get_company_id')

    def get_company_id(self, name):
        if self.quotation:
            return self.quotation.proposal.client.company
        else:
            return None

My objective is to show a list of Company’s addresses, based on selected Company (which the quotation belongs to).
My steps:

  1. User select a quotation from Many2One selection
  2. Then select a company address from Many2One selection, with domain restriction based on selected quotation’s company.
  3. So I point the Eval to a function field → company_id that called selected quotation’s company

But when I do console logging at CompanyAddress class, what I got is like below:

INFO:trytond.modules.ltl_company.model.company_address:>>>>>>>>>>>>>> [['company', '=', None]] [company_address.py:46]

The company_id field Function seems doesn’t work. Does anybody knows how the right way to do this?

Bromo

The company_id field being a Function field with a simple getter, it gets updated only when read (so on load or reload). But if you want to have accurate value while being edited, you need to use a getter that is always an on_change_with method.

1 Like