跳至內容
選單
此問題已被標幟
2 回覆
93 瀏覽次數

Hello, I have added the field analytic_distribution (journal item) to a report and need to know how to show the name rather than the code via a computed field. Any help appreciated.


For example the field shows:

Can anyone tell me the code I need to add to a computed field called "x_studio_analytic_distribution"

頭像
捨棄

FYI. The Related Field definition will return the Analytic Distribution of just the first (by ID) Journal Item. What happens when there are multiple lines with distributions or when the first line doesn't have one?

最佳答案

Hi,

Please refer to the code:

import json


for record in self:

    names = []

    for line in record.line_ids:

        if line.analytic_distribution:

            data = json.loads(line.analytic_distribution)

            for analytic_id in data.keys():

                analytic = record.env['account.analytic.account'].browse(int(analytic_id))

                if analytic.exists():

                    names.append(analytic.name)

    record['x_studio_analytic_distribution'] = ', '.join(names)


Hope it helps.

頭像
捨棄
最佳答案

@api.depends('analytic_distribution')

    def _compute_x_studio_analytic_distribution(self):

        for line in self:

            if line.analytic_distribution:

                analytic_ids = list(line.analytic_distribution.keys())

                names = self.env['account.analytic.account'].browse(analytic_ids).mapped('name')

                line.x_studio_analytic_distribution = ', '.join(names)

            else:

                line.x_studio_analytic_distribution = ''

頭像
捨棄